Skip to content

BUG: .max() gives wrong answer for non-nanosecond datetimeindex #50719

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

Merged
merged 5 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion pandas/core/nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ def _wrap_results(result, dtype: np.dtype, fill_value=None):
if isna(result):
result = np.datetime64("NaT", "ns")
else:
result = np.int64(result).view("datetime64[ns]")
result = np.int64(result).view(dtype)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you do the nat case a couple lines up too just to be on the safe side (like we do for td64 below)

# retain original unit
result = result.astype(dtype, copy=False)
else:
Expand Down
13 changes: 9 additions & 4 deletions pandas/tests/test_nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1157,10 +1157,14 @@ def prng(self):


class TestDatetime64NaNOps:
@pytest.fixture(params=["s", "ms", "us", "ns"])
def unit(self, request):
return request.param

# Enabling mean changes the behavior of DataFrame.mean
# See https://github.com/pandas-dev/pandas/issues/24752
def test_nanmean(self):
dti = pd.date_range("2016-01-01", periods=3)
def test_nanmean(self, unit):
dti = pd.date_range("2016-01-01", periods=3).as_unit(unit)
expected = dti[1]

for obj in [dti, DatetimeArray(dti), Series(dti)]:
Expand All @@ -1173,8 +1177,9 @@ def test_nanmean(self):
result = nanops.nanmean(obj)
assert result == expected

@pytest.mark.parametrize("dtype", ["M8[ns]", "m8[ns]"])
def test_nanmean_skipna_false(self, dtype):
@pytest.mark.parametrize("constructor", ["M8", "m8"])
def test_nanmean_skipna_false(self, constructor, unit):
dtype = f"{constructor}[{unit}]"
arr = np.arange(12).astype(np.int64).view(dtype).reshape(4, 3)

arr[-1, -1] = "NaT"
Expand Down