Skip to content
Open
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
17 changes: 17 additions & 0 deletions pandas/tests/dtypes/test_dtypes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from contextlib import redirect_stdout
import io
import re
import warnings
import weakref
Expand Down Expand Up @@ -1259,3 +1261,18 @@ def test_categorical_nan_no_dtype_conversion():
expected = pd.DataFrame({"a": Categorical([1], [1]), "b": [1]})
df.loc[0, "a"] = np.array([1])
tm.assert_frame_equal(df, expected)


def test_dt_to_pydatetime_conversion():
Copy link
Member

Choose a reason for hiding this comment

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

is this test about info or the df["ts"] = call? that will determine what file this belongs in

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is about info()
As described in the issue: a df has a column of type datetime[ns]. After to_pydatetime conversion, the reporter of the issue expects df.info() will show the column changes to datetime type, but still got datetime[ns] type.

expected = "datetime"

df = pd.DataFrame({"ts": [pd.Timestamp("2024-01-01 12:00:00").as_unit("ns")]})
df["ts"] = df.ts.dt.to_pydatetime()
assert df["ts"].dtype == expected

buffer = io.StringIO()
with redirect_stdout(buffer):
df.info()

output = buffer.getvalue()
assert expected in output
Copy link
Member

Choose a reason for hiding this comment

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

i think you'll end up with "object" in output

Copy link
Contributor Author

@litang99 litang99 Nov 12, 2025

Choose a reason for hiding this comment

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

Yes. Different versions give different result.s
In the issue description, it expects datetime. But the latest version in the main branch output "object".
What should be correct value?
I am not sure if the latest main has a bug so that the output is "object" instead of "datetime".

Loading