Skip to content

Commit 7078607

Browse files
phoflmroeschke
andauthored
Backport PR #48820 on branch 1.5.x (BUG: to_datetime(format='...%f') parses nanoseconds) (#48860)
BUG: to_datetime(format='...%f') parses nanoseconds (#48820) Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 2e3a40a commit 7078607

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

doc/source/whatsnew/v1.5.1.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,10 @@ Fixed regressions
7575
- Fixed regression in :meth:`DataFrame.describe` raising ``TypeError`` when result contains ``NA`` (:issue:`48778`)
7676
- Fixed regression in :meth:`DataFrame.plot` ignoring invalid ``colormap`` for ``kind="scatter"`` (:issue:`48726`)
7777
- Fixed performance regression in :func:`factorize` when ``na_sentinel`` is not ``None`` and ``sort=False`` (:issue:`48620`)
78-
- Fixed Regression in :meth:`DataFrameGroupBy.apply` when user defined function is called on an empty dataframe (:issue:`47985`)
78+
- Fixed regression causing an ``AttributeError`` during warning emitted if the provided table name in :meth:`DataFrame.to_sql` and the table name actually used in the database do not match (:issue:`48733`)
79+
- Fixed regression in :func:`to_datetime` when ``arg`` was a date string with nanosecond and ``format`` contained ``%f`` would raise a ``ValueError`` (:issue:`48767`)
7980
- Fixed :meth:`.DataFrameGroupBy.size` not returning a Series when ``axis=1`` (:issue:`48738`)
81+
- Fixed Regression in :meth:`DataFrameGroupBy.apply` when user defined function is called on an empty dataframe (:issue:`47985`)
8082
-
8183

8284
.. ---------------------------------------------------------------------------

pandas/_libs/tslibs/strptime.pyx

+4-1
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ def array_strptime(ndarray[object] values, str fmt, bint exact=True, errors='rai
351351
"""
352352
TimeRE, _calc_julian_from_U_or_W are vendored
353353
from the standard library, see
354-
https://github.com/python/cpython/blob/master/Lib/_strptime.py
354+
https://github.com/python/cpython/blob/main/Lib/_strptime.py
355355
The original module-level docstring follows.
356356
357357
Strptime-related classes and functions.
@@ -387,6 +387,9 @@ class TimeRE(_TimeRE):
387387
"""
388388
self._Z = None
389389
super().__init__(locale_time=locale_time)
390+
# GH 48767: Overrides for cpython's TimeRE
391+
# 1) Parse up to nanos instead of micros
392+
self.update({"f": r"(?P<f>[0-9]{1,9})"}),
390393

391394
def __getitem__(self, key):
392395
if key == "Z":

pandas/tests/tools/test_to_datetime.py

+18
Original file line numberDiff line numberDiff line change
@@ -2815,3 +2815,21 @@ def test_to_datetime_cache_coerce_50_lines_outofbounds(series_length):
28152815

28162816
with pytest.raises(OutOfBoundsDatetime, match="Out of bounds nanosecond timestamp"):
28172817
to_datetime(s, errors="raise", utc=True)
2818+
2819+
2820+
def test_to_datetime_format_f_parse_nanos():
2821+
# GH 48767
2822+
timestamp = "15/02/2020 02:03:04.123456789"
2823+
timestamp_format = "%d/%m/%Y %H:%M:%S.%f"
2824+
result = to_datetime(timestamp, format=timestamp_format)
2825+
expected = Timestamp(
2826+
year=2020,
2827+
month=2,
2828+
day=15,
2829+
hour=2,
2830+
minute=3,
2831+
second=4,
2832+
microsecond=123456,
2833+
nanosecond=789,
2834+
)
2835+
assert result == expected

0 commit comments

Comments
 (0)