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

BUG: DataFrame reductions casting ts resolution always to nanoseconds #52566

Merged
merged 6 commits into from
Apr 12, 2023
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 doc/source/whatsnew/v2.0.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Bug fixes
- Bug in :meth:`Series.describe` not returning :class:`ArrowDtype` with ``pyarrow.float64`` type with numeric data (:issue:`52427`)
- Fixed segfault in :meth:`Series.to_numpy` with ``null[pyarrow]`` dtype (:issue:`52443`)
- 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`)
- Bug in :meth:`DataFrame.max` and related casting different :class:`Timestamp` resolutions always to nanoseconds (:issue:`52524`)
- Bug in :meth:`ArrowDtype.__from_arrow__` not respecting if dtype is explicitly given (:issue:`52533`)
- 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`)

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1408,9 +1408,9 @@ def find_common_type(types):

# take lowest unit
if all(is_datetime64_dtype(t) for t in types):
return np.dtype("datetime64[ns]")
return np.dtype(max(types))
if all(is_timedelta64_dtype(t) for t in types):
return np.dtype("timedelta64[ns]")
return np.dtype(max(types))

# don't mix bool / int or float or complex
# this is different from numpy, which casts bool with float/int as int
Expand Down
36 changes: 36 additions & 0 deletions pandas/tests/frame/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1507,6 +1507,42 @@ def test_reductions_skipna_none_raises(
with pytest.raises(ValueError, match=msg):
getattr(obj, all_reductions)(skipna=None)

@td.skip_array_manager_invalid_test
def test_reduction_timestamp_smallest_unit(self):
# GH#52524
df = DataFrame(
{
"a": Series([Timestamp("2019-12-31")], dtype="datetime64[s]"),
"b": Series(
[Timestamp("2019-12-31 00:00:00.123")], dtype="datetime64[ms]"
),
}
)
result = df.max()
expected = Series(
[Timestamp("2019-12-31"), Timestamp("2019-12-31 00:00:00.123")],
dtype="datetime64[ms]",
index=["a", "b"],
)
tm.assert_series_equal(result, expected)

@td.skip_array_manager_not_yet_implemented
def test_reduction_timedelta_smallest_unit(self):
# GH#52524
df = DataFrame(
{
"a": Series([pd.Timedelta("1 days")], dtype="timedelta64[s]"),
"b": Series([pd.Timedelta("1 days")], dtype="timedelta64[ms]"),
}
)
result = df.max()
expected = Series(
[pd.Timedelta("1 days"), pd.Timedelta("1 days")],
dtype="timedelta64[ms]",
index=["a", "b"],
)
tm.assert_series_equal(result, expected)


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