Skip to content

Commit 1804780

Browse files
Backport PR pandas-dev#44828 on branch 1.3.x (REGR: resampling DataFrame with DateTimeIndex with empty groups and uint8, uint16 or uint32 columns incorrectly raising RuntimeError) (pandas-dev#44831)
Co-authored-by: Simon Hawkins <[email protected]>
1 parent f3bcf95 commit 1804780

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

doc/source/whatsnew/v1.3.5.rst

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Fixed regressions
1616
~~~~~~~~~~~~~~~~~
1717
- Fixed regression in :meth:`Series.equals` when comparing floats with dtype object to None (:issue:`44190`)
1818
- Fixed regression in :func:`merge_asof` raising error when array was supplied as join key (:issue:`42844`)
19+
- Fixed regression when resampling :class:`DataFrame` with :class:`DateTimeIndex` with empty groups and ``uint8``, ``uint16`` or ``uint32`` columns incorrectly raising ``RuntimeError`` (:issue:`43329`)
1920
- Fixed regression in creating a :class:`DataFrame` from a timezone-aware :class:`Timestamp` scalar near a Daylight Savings Time transition (:issue:`42505`)
2021
- Fixed performance regression in :func:`read_csv` (:issue:`44106`)
2122
- Fixed regression in :meth:`Series.duplicated` and :meth:`Series.drop_duplicates` when Series has :class:`Categorical` dtype with boolean categories (:issue:`44351`)

pandas/core/groupby/ops.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -546,9 +546,10 @@ def _call_cython_op(
546546
elif is_bool_dtype(dtype):
547547
values = values.astype("int64")
548548
elif is_integer_dtype(dtype):
549-
# e.g. uint8 -> uint64, int16 -> int64
550-
dtype_str = dtype.kind + "8"
551-
values = values.astype(dtype_str, copy=False)
549+
# GH#43329 If the dtype is explicitly of type uint64 the type is not
550+
# changed to prevent overflow.
551+
if dtype != np.uint64:
552+
values = values.astype(np.int64, copy=False)
552553
elif is_numeric:
553554
if not is_complex_dtype(dtype):
554555
values = ensure_float64(values)

pandas/tests/resample/test_datetime_index.py

+24
Original file line numberDiff line numberDiff line change
@@ -1827,3 +1827,27 @@ def test_resample_aggregate_functions_min_count(func):
18271827
index=DatetimeIndex(["2020-03-31"], dtype="datetime64[ns]", freq="Q-DEC"),
18281828
)
18291829
tm.assert_series_equal(result, expected)
1830+
1831+
1832+
def test_resample_unsigned_int(uint_dtype):
1833+
# gh-43329
1834+
df = DataFrame(
1835+
index=date_range(start="2000-01-01", end="2000-01-03 23", freq="12H"),
1836+
columns=["x"],
1837+
data=[0, 1, 0] * 2,
1838+
dtype=uint_dtype,
1839+
)
1840+
df = df.loc[(df.index < "2000-01-02") | (df.index > "2000-01-03"), :]
1841+
1842+
if uint_dtype == "uint64":
1843+
with pytest.raises(RuntimeError, match="empty group with uint64_t"):
1844+
result = df.resample("D").max()
1845+
else:
1846+
result = df.resample("D").max()
1847+
1848+
expected = DataFrame(
1849+
[1, np.nan, 0],
1850+
columns=["x"],
1851+
index=date_range(start="2000-01-01", end="2000-01-03 23", freq="D"),
1852+
)
1853+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)