Skip to content

Commit

Permalink
- updated _metadata_to_df function (#70)
Browse files Browse the repository at this point in the history
Bug description:
write_csv encounters TypeError when metadata is provided in dict format.

Solution:
edited write_csv function that features press_element" function that
loops over each element and handles lists, dicts, and strings, or
"return as is".
Closes #69 

Error message:

TypeError Traceback (most recent call last)
in <cell line: 2>()
1 # Save the corpus as a .csv file locally
----> 2 Dutch_corpus.write_csv(path = "Dutch_corpus.csv")

8 frames

[/usr/local/lib/python3.10/dist-packages/sktalk/corpus/write/writer.py](https://46yu2lzt3ep-496ff2e9c6d22116-0-colab.googleusercontent.com/outputframe.html?vrz=colab_20240830-060131_RC00_669227538#)
in (x)
52 norm = pd.json_normalize(data=metadata, sep="_")
53 df = pd.DataFrame(norm)
---> 54 df[:] = np.vectorize(lambda x: ', '.join(
55 x) if isinstance(x, list) else x)(df)
56 return df

TypeError: sequence item 0: expected str instance, dict found

Key Changes:
Added process_element function: This function handles three cases:
List: Joins the elements with ', '.
Dictionary: Converts the dictionary to a JSON string using json.dumps.
Alternatively, you could convert the dictionary to a custom string
format, e.g., by joining key-value pairs with a colon.
Other types: Returns the value as-is.
Replaced lambda with process_element: The np.vectorize now applies this
more robust function to each element in the DataFrame.
This approach should resolve the TypeError by correctly handling cases
where elements in the DataFrame are dictionaries.
  • Loading branch information
liesenf authored Sep 4, 2024
1 parent 2424d62 commit 6fbe8f9
Show file tree
Hide file tree
Showing 2 changed files with 461 additions and 2 deletions.
Loading

0 comments on commit 6fbe8f9

Please sign in to comment.