Skip to content

Commit

Permalink
FIX-#7371: Fix inserting datelike values into a DataFrame (#7372)
Browse files Browse the repository at this point in the history
Signed-off-by: Jonathan Shi <[email protected]>
  • Loading branch information
noloerino authored Aug 28, 2024
1 parent 8249915 commit f70176a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
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)

0 comments on commit f70176a

Please sign in to comment.