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-#4966: Fix to_timedelta to return Series instead of TimedeltaIndex #5028

Merged
merged 12 commits into from
Oct 4, 2022
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
1 change: 1 addition & 0 deletions docs/release_notes/release_notes-0.16.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Key Features and Updates
* FIX-#4996: Evaluate BenchmarkMode at each function call (#4997)
* FIX-#4022: Fixed empty data frame with index (#4910)
* FIX-#4090: Fixed check if the index is trivial (#4936)
* FIX-#4966: Fix `to_timedelta` to return Series instead of TimedeltaIndex (#5028)
* FIX-#5042: Fix series __getitem__ with invalid strings (#5048)
* FIX-#4691: Fix binary operations between virtual partitions (#5049)
* FIX-#5045: Fix ray virtual_partition.wait with duplicate object refs (#5058)
Expand Down
21 changes: 21 additions & 0 deletions modin/core/storage_formats/base/query_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1320,6 +1320,27 @@ def to_numeric(self, *args, **kwargs): # noqa: PR02
"""
return SeriesDefault.register(pandas.to_numeric)(self, *args, **kwargs)

@doc_utils.add_one_column_warning
@doc_utils.add_refer_to("to_timedelta")
def to_timedelta(self, unit="ns", errors="raise"): # noqa: PR02
"""
Convert argument to timedelta.

Parameters
----------
unit : str, default: "ns"
Denotes the unit of the arg for numeric arg. Defaults to "ns".
errors : {"ignore", "raise", "coerce"}, default: "raise"

Returns
-------
BaseQueryCompiler
New QueryCompiler with converted to timedelta values.
"""
return SeriesDefault.register(pandas.to_timedelta)(
self, unit=unit, errors=errors
)

# FIXME: get rid of `**kwargs` parameter (Modin issue #3108).
@doc_utils.add_one_column_warning
@doc_utils.add_refer_to("Series.unique")
Expand Down
6 changes: 6 additions & 0 deletions modin/core/storage_formats/pandas/query_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1360,6 +1360,12 @@ def stack(self, level, dropna):
pandas.to_numeric(df.squeeze(axis=1), *args, **kwargs)
)
)
to_timedelta = Map.register(
billiam-wang marked this conversation as resolved.
Show resolved Hide resolved
lambda s, *args, **kwargs: pandas.to_timedelta(
s.squeeze(axis=1), *args, **kwargs
).to_frame(),
dtypes="timedelta64[ns]",
)

# END Map partitions operations

Expand Down
2 changes: 1 addition & 1 deletion modin/pandas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
DatetimeIndex,
Timedelta,
Timestamp,
to_timedelta,
set_eng_float_format,
options,
set_option,
Expand Down Expand Up @@ -251,6 +250,7 @@ def init_remote_ray(partition):
crosstab,
lreshape,
wide_to_long,
to_timedelta,
)

from modin._compat.pandas_api.namespace import pivot_table
Expand Down
15 changes: 15 additions & 0 deletions modin/pandas/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -759,3 +759,18 @@ def get_names(obj):
return list(names[0]) if is_list_like(names[0]) else [names[0]]
else:
return None


@_inherit_docstrings(pandas.to_datetime, apilink="pandas.to_timedelta")
@enable_logging
def to_timedelta(arg, unit=None, errors="raise"): # noqa: PR01, RT01, D200
"""
Convert argument to timedelta.

Accepts str, timedelta, list-like or Series for arg parameter.
Returns a Series if and only if arg is provided as a Series.
"""
if isinstance(arg, Series):
query_compiler = arg._query_compiler.to_timedelta(unit=unit, errors=errors)
return Series(query_compiler=query_compiler)
return pandas.to_timedelta(arg, unit=unit, errors=errors)
billiam-wang marked this conversation as resolved.
Show resolved Hide resolved
billiam-wang marked this conversation as resolved.
Show resolved Hide resolved
23 changes: 23 additions & 0 deletions modin/pandas/test/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
test_data_keys,
df_equals,
sort_index_for_equal_values,
eval_general,
)


Expand Down Expand Up @@ -801,3 +802,25 @@ def test_empty_dataframe():
def test_empty_series():
s = pd.Series([])
pd.to_numeric(s)


@pytest.mark.parametrize(
"arg",
[[1, 2], ["a"], 1, "a"],
ids=["list_of_ints", "list_of_invalid_strings", "scalar", "invalid_scalar"],
)
def test_to_timedelta(arg):
# This test case comes from
# https://github.com/modin-project/modin/issues/4966
eval_general(pd, pandas, lambda lib: lib.to_timedelta(arg))


@pytest.mark.parametrize("data", test_data_values, ids=test_data_keys)
def test_series_to_timedelta(data):
def make_frame(lib):
series = lib.Series(
next(iter(data.values())) if isinstance(data, dict) else data
)
return lib.to_timedelta(series).to_frame(name="timedelta")

eval_general(pd, pandas, make_frame)