Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX-#7371: Fix inserting datelike values into a DataFrame #7372

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion modin/core/dataframe/pandas/metadata/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1225,7 +1225,7 @@ def extract_dtype(value) -> DtypeObj | pandas.Series:
"""
try:
dtype = pandas.api.types.pandas_dtype(value)
except TypeError:
except (TypeError, ValueError):
dtype = pandas.Series(value).dtype

return dtype
15 changes: 15 additions & 0 deletions modin/tests/pandas/dataframe/test_map_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -1837,3 +1837,18 @@ def test_constructor_from_index():
data = pd.Index([1, 2, 3], name="pricing_date")
modin_df, pandas_df = create_test_dfs(data)
df_equals(modin_df, pandas_df)


def test_insert_datelike_string_issue_7371():
# When a new value is inserted into a frame, we call pandas.api.types.pandas_dtype(value) to
# extract the dtype of an object like a pandas Series or numpy array. When a scalar value is passed,
# this usually raises a TypeError, so we construct a local pandas Series from the object and
# extract the dtype from there.
# When the passed value is a date-like string, pandas will instead raise a ValueError because
# it tries to parse it as a numpy structured dtype. After fixing GH#7371, we now catch
# ValueError in addition to TypeError to handle this case.
modin_df = pd.DataFrame({"a": [0]})
modin_df["c"] = "2020-01-01"
pandas_df = pandas.DataFrame({"a": [0]})
pandas_df["c"] = "2020-01-01"
df_equals(modin_df, pandas_df)
Loading