Skip to content

Nanosecond fixed precision of DatetimeIndex with time zone information, Fix #53473 #57703

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

Closed
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
4 changes: 3 additions & 1 deletion pandas/_libs/src/datetime/pd_datetime.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ static int convert_pydatetime_to_datetimestruct(PyObject *dtobj,
out->min = PyLong_AsLong(PyObject_GetAttrString(obj, "minute"));
out->sec = PyLong_AsLong(PyObject_GetAttrString(obj, "second"));
out->us = PyLong_AsLong(PyObject_GetAttrString(obj, "microsecond"));

if (PyObject_HasAttrString(obj, "nanosecond")) {
out->ps = 1000 * PyLong_AsLong(PyObject_GetAttrString(obj, "nanosecond"));
}
if (PyObject_HasAttrString(obj, "tzinfo")) {
PyObject *offset = extract_utc_offset(obj);
/* Apply the time zone offset if datetime obj is tz-aware */
Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/io/json/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,26 @@ def test_datetime_tz(self):
s_naive = Series(tz_naive)
assert stz.to_json() == s_naive.to_json()

def test_tz_nano_datetimes(self):
df = DataFrame(
{
"date": Series(
[
datetime.datetime(
2024, 1, 1, 0, 0, 0, 000000, tzinfo=datetime.timezone.utc
)
]
)
}
)
df.date = df.date + np.timedelta64(1, "ns")
buf = StringIO()
df.to_json(buf, date_unit="ns", orient="columns", date_format="iso")
buf.seek(0)
tm.assert_frame_equal(
read_json(buf), df, check_index_type=False, check_dtype=False
)

def test_sparse(self):
# GH4377 df.to_json segfaults with non-ndarray blocks
df = DataFrame(np.random.default_rng(2).standard_normal((10, 4)))
Expand Down