Skip to content

Commit cfbbeb6

Browse files
authored
BUG: DataFrame reductions casting ts resolution always to nanoseconds (#52566)
1 parent e8416df commit cfbbeb6

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

doc/source/whatsnew/v2.0.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Bug fixes
2828
- Bug in :meth:`Series.describe` not returning :class:`ArrowDtype` with ``pyarrow.float64`` type with numeric data (:issue:`52427`)
2929
- Fixed segfault in :meth:`Series.to_numpy` with ``null[pyarrow]`` dtype (:issue:`52443`)
3030
- Bug in :func:`pandas.testing.assert_series_equal` where ``check_dtype=False`` would still raise for datetime or timedelta types with different resolutions (:issue:`52449`)
31+
- Bug in :meth:`DataFrame.max` and related casting different :class:`Timestamp` resolutions always to nanoseconds (:issue:`52524`)
3132
- Bug in :meth:`ArrowDtype.__from_arrow__` not respecting if dtype is explicitly given (:issue:`52533`)
3233
- Bug in :func:`read_csv` casting PyArrow datetimes to NumPy when ``dtype_backend="pyarrow"`` and ``parse_dates`` is set causing a performance bottleneck in the process (:issue:`52546`)
3334
- Bug in :class:`arrays.DatetimeArray` constructor returning an incorrect unit when passed a non-nanosecond numpy datetime array (:issue:`52555`)

pandas/core/dtypes/cast.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1408,9 +1408,9 @@ def find_common_type(types):
14081408

14091409
# take lowest unit
14101410
if all(is_datetime64_dtype(t) for t in types):
1411-
return np.dtype("datetime64[ns]")
1411+
return np.dtype(max(types))
14121412
if all(is_timedelta64_dtype(t) for t in types):
1413-
return np.dtype("timedelta64[ns]")
1413+
return np.dtype(max(types))
14141414

14151415
# don't mix bool / int or float or complex
14161416
# this is different from numpy, which casts bool with float/int as int

pandas/tests/frame/test_reductions.py

+36
Original file line numberDiff line numberDiff line change
@@ -1507,6 +1507,42 @@ def test_reductions_skipna_none_raises(
15071507
with pytest.raises(ValueError, match=msg):
15081508
getattr(obj, all_reductions)(skipna=None)
15091509

1510+
@td.skip_array_manager_invalid_test
1511+
def test_reduction_timestamp_smallest_unit(self):
1512+
# GH#52524
1513+
df = DataFrame(
1514+
{
1515+
"a": Series([Timestamp("2019-12-31")], dtype="datetime64[s]"),
1516+
"b": Series(
1517+
[Timestamp("2019-12-31 00:00:00.123")], dtype="datetime64[ms]"
1518+
),
1519+
}
1520+
)
1521+
result = df.max()
1522+
expected = Series(
1523+
[Timestamp("2019-12-31"), Timestamp("2019-12-31 00:00:00.123")],
1524+
dtype="datetime64[ms]",
1525+
index=["a", "b"],
1526+
)
1527+
tm.assert_series_equal(result, expected)
1528+
1529+
@td.skip_array_manager_not_yet_implemented
1530+
def test_reduction_timedelta_smallest_unit(self):
1531+
# GH#52524
1532+
df = DataFrame(
1533+
{
1534+
"a": Series([pd.Timedelta("1 days")], dtype="timedelta64[s]"),
1535+
"b": Series([pd.Timedelta("1 days")], dtype="timedelta64[ms]"),
1536+
}
1537+
)
1538+
result = df.max()
1539+
expected = Series(
1540+
[pd.Timedelta("1 days"), pd.Timedelta("1 days")],
1541+
dtype="timedelta64[ms]",
1542+
index=["a", "b"],
1543+
)
1544+
tm.assert_series_equal(result, expected)
1545+
15101546

15111547
class TestNuisanceColumns:
15121548
@pytest.mark.parametrize("method", ["any", "all"])

0 commit comments

Comments
 (0)