Skip to content

ENH: Add support for more placeholders in guess_datetime_format #43900

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 1 commit into from
Oct 17, 2021
Merged
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
10 changes: 10 additions & 0 deletions asv_bench/benchmarks/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,16 @@ def time_dup_string_tzoffset_dates(self, cache):
to_datetime(self.dup_string_with_tz, cache=cache)


# GH 43901
class ToDatetimeInferDatetimeFormat:
def setup(self):
rng = date_range(start="1/1/2000", periods=100000, freq="H")
self.strings = rng.strftime("%Y-%m-%d %H:%M:%S").tolist()

def time_infer_datetime_format(self):
to_datetime(self.strings, infer_datetime_format=True)


class ToTimedelta:
def setup(self):
self.ints = np.random.randint(0, 60, size=10000)
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ Performance improvements
- Performance improvement in some :meth:`GroupBy.apply` operations (:issue:`42992`)
- Performance improvement in :func:`read_stata` (:issue:`43059`)
- Performance improvement in :meth:`to_datetime` with ``uint`` dtypes (:issue:`42606`)
- Performance improvement in :meth:`to_datetime` with ``infer_datetime_format`` set to ``True`` (:issue:`43901`)
- Performance improvement in :meth:`Series.sparse.to_coo` (:issue:`42880`)
- Performance improvement in indexing with a :class:`MultiIndex` indexer on another :class:`MultiIndex` (:issue:43370`)
- Performance improvement in :meth:`GroupBy.quantile` (:issue:`43469`)
Expand Down
23 changes: 14 additions & 9 deletions pandas/_libs/tslibs/parsing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,9 @@ def guess_datetime_format(
(('second', 'microsecond'), '%S.%f', 0),
(('tzinfo',), '%z', 0),
(('tzinfo',), '%Z', 0),
(('day_of_week',), '%a', 0),
(('day_of_week',), '%A', 0),
(('meridiem',), '%p', 0),
]

if dayfirst:
Expand Down Expand Up @@ -967,15 +970,17 @@ def guess_datetime_format(
if set(attrs) & found_attrs:
continue

if all(getattr(parsed_datetime, attr) is not None for attr in attrs):
for i, token_format in enumerate(format_guess):
token_filled = tokens[i].zfill(padding)
if (token_format is None and
token_filled == parsed_datetime.strftime(attr_format)):
format_guess[i] = attr_format
tokens[i] = token_filled
found_attrs.update(attrs)
break
if parsed_datetime.tzinfo is None and attr_format in ("%Z", "%z"):
continue

parsed_formatted = parsed_datetime.strftime(attr_format)
for i, token_format in enumerate(format_guess):
token_filled = tokens[i].zfill(padding)
if token_format is None and token_filled == parsed_formatted:
format_guess[i] = attr_format
tokens[i] = token_filled
found_attrs.update(attrs)
break

# Only consider it a valid guess if we have a year, month and day
if len({'year', 'month', 'day'} & found_attrs) != 3:
Expand Down
2 changes: 2 additions & 0 deletions pandas/tests/tslibs/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ def test_parsers_month_freq(date_str, expected):
("2011-12-30T00:00:00.000000+9:0", "%Y-%m-%dT%H:%M:%S.%f%z"),
("2011-12-30T00:00:00.000000+09:", None),
("2011-12-30 00:00:00.000000", "%Y-%m-%d %H:%M:%S.%f"),
("Tue 24 Aug 2021 01:30:48 AM", "%a %d %b %Y %H:%M:%S %p"),
("Tuesday 24 Aug 2021 01:30:48 AM", "%A %d %b %Y %H:%M:%S %p"),
],
)
def test_guess_datetime_format_with_parseable_formats(string, fmt):
Expand Down