Skip to content

ENH: DatetimeProperties results seem to be inconsistent since missing milliseconds #49073 #49719

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
wants to merge 2 commits into from
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
11 changes: 11 additions & 0 deletions pandas/_libs/tslibs/fields.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,17 @@ def get_date_field(
out[i] = dts.us
return out

elif field == 'ms':
with nogil:
for i in range(count):
if dtindex[i] == NPY_NAT:
out[i] = -1
continue

pandas_datetime_to_datetimestruct(dtindex[i], reso, &dts)
out[i] = dts.us // 1000
return out

elif field == 'ns':
with nogil:
for i in range(count):
Expand Down
25 changes: 25 additions & 0 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ def _scalar_type(self) -> type[Timestamp]:
"quarter",
"days_in_month",
"daysinmonth",
"millisecond",
"microsecond",
"nanosecond",
]
Expand Down Expand Up @@ -1475,6 +1476,29 @@ def isocalendar(self) -> DataFrame:
dtype: int64
""",
)
millisecond = _field_accessor(
"millisecond",
"ms",
"""
The milliseconds of the datetime.

Examples
--------
>>> datetime_series = pd.Series(
... pd.date_range("2000-01-01", periods=3, freq="us")
... )
>>> datetime_series
0 2000-01-01 00:00:00.000
1 2000-01-01 00:00:00.001
2 2000-01-01 00:00:00.002
dtype: datetime64[ns]
>>> datetime_series.dt.millisecond
0 0
1 1
2 2
dtype: int64
""",
)
microsecond = _field_accessor(
"microsecond",
"us",
Expand Down Expand Up @@ -1874,6 +1898,7 @@ def to_julian_date(self) -> npt.NDArray[np.float64]:
self.hour
+ self.minute / 60
+ self.second / 3600
+ self.millisecond / 3600 / 10**3
Copy link
Member

Choose a reason for hiding this comment

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

Why was this added? I think self.microsecond should already include the milliseconds

+ self.microsecond / 3600 / 10**6
+ self.nanosecond / 3600 / 10**9
)
Expand Down