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 millisecond resampling in cudf Python #15560

Merged
merged 2 commits into from
Apr 18, 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
44 changes: 21 additions & 23 deletions python/cudf/cudf/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
SeriesGroupBy,
_Grouping,
)
from cudf.core.tools.datetimes import _offset_alias_to_code, _unit_dtype_map


class _Resampler(GroupBy):
Expand Down Expand Up @@ -247,47 +246,46 @@ def _handle_frequency_grouper(self, by):
# column to have the same dtype, so we compute a `result_type`
# and cast them both to that type.
try:
result_type = np.dtype(
_unit_dtype_map[_offset_alias_to_code[offset.name]]
)
except KeyError:
result_type = np.dtype(f"datetime64[{offset.rule_code}]")
# TODO: Ideally, we can avoid one cast by having `date_range`
# generate timestamps of a given dtype. Currently, it can
# only generate timestamps with 'ns' precision
cast_key_column = key_column.astype(result_type)
cast_bin_labels = bin_labels.astype(result_type)
except TypeError:
# unsupported resolution (we don't support resolutions >s)
# fall back to using datetime64[s]
result_type = np.dtype("datetime64[s]")

# TODO: Ideally, we can avoid one cast by having `date_range`
# generate timestamps of a given dtype. Currently, it can
# only generate timestamps with 'ns' precision
key_column = key_column.astype(result_type)
bin_labels = bin_labels.astype(result_type)
cast_key_column = key_column.astype(result_type)
cast_bin_labels = bin_labels.astype(result_type)

# bin the key column:
bin_numbers = cudf._lib.labeling.label_bins(
key_column,
left_edges=bin_labels[:-1]._column,
cast_key_column,
left_edges=cast_bin_labels[:-1]._column,
left_inclusive=(closed == "left"),
right_edges=bin_labels[1:]._column,
right_edges=cast_bin_labels[1:]._column,
right_inclusive=(closed == "right"),
)

if label == "right":
bin_labels = bin_labels[1:]
cast_bin_labels = cast_bin_labels[1:]
else:
bin_labels = bin_labels[:-1]
cast_bin_labels = cast_bin_labels[:-1]

# if we have more labels than bins, remove the extras labels:
nbins = bin_numbers.max() + 1
if len(bin_labels) > nbins:
bin_labels = bin_labels[:nbins]
if len(cast_bin_labels) > nbins:
cast_bin_labels = cast_bin_labels[:nbins]

bin_labels.name = self.names[0]
self.bin_labels = bin_labels
cast_bin_labels.name = self.names[0]
self.bin_labels = cast_bin_labels

# replace self._key_columns with the binned key column:
self._key_columns = [
bin_labels._gather(bin_numbers, check_bounds=False)._column.astype(
result_type
)
cast_bin_labels._gather(
bin_numbers, check_bounds=False
)._column.astype(result_type)
]


Expand Down
15 changes: 0 additions & 15 deletions python/cudf/cudf/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,6 @@
"D": "datetime64[s]",
}

_offset_alias_to_code = {
"W": "W",
"D": "D",
"H": "h",
"h": "h",
"T": "m",
"min": "m",
"s": "s",
"S": "s",
"U": "us",
"us": "us",
"N": "ns",
"ns": "ns",
}


def to_datetime(
arg,
Expand Down
14 changes: 14 additions & 0 deletions python/cudf/cudf/tests/test_resampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,17 @@ def test_resampling_frequency_conversion(in_freq, sampling_freq, out_freq):
assert_resample_results_equal(expect, got)

assert got.index.dtype == np.dtype(f"datetime64[{out_freq}]")


def test_resampling_downsampling_ms():
pdf = pd.DataFrame(
{
"time": pd.date_range("2020-01-01", periods=5, freq="1ns"),
"sign": range(5),
}
)
gdf = cudf.from_pandas(pdf)
expected = pdf.resample("10ms", on="time").mean()
result = gdf.resample("10ms", on="time").mean()
result.index = result.index.astype("datetime64[ns]")
assert_eq(result, expected, check_freq=False)
Loading