Skip to content

Commit beed6bc

Browse files
nnlnrDonald ThevalingamMarcoGorelli
authored
ENH: make guess_datetime_format public (pandas-dev#55079)
* updating init to include guess_datetime_format func * included guess_datetime_format in api * updated test to include guess_datetime_format method * ran pre-commit hook * updated latest rst doc with enhancement note * removed enhancement update * alphabetize other-enhancement list * added guess_datetime_format * updated guess_datetime_format docstring with warning sphinx directive * fixed spelling from pre-commit hook * update guess_datetime_format docstring examples * fixed trailing whitespace * correct sphinx directive for guess_datetime_format with trailing whitespace * updated whatsnew with corrected guess_datatime_format api method string * removed extra line * removed comment lines in example for guess_datetime_format * removed api from method call for guess_datetime_format * pre-commit hook fix for whitespace * fix extra char * fix whitespace * removed toctree api * fixup * try fixing docstring * add whatsnew note --------- Co-authored-by: Donald Thevalingam <[email protected]> Co-authored-by: MarcoGorelli <[email protected]>
1 parent af72b63 commit beed6bc

File tree

7 files changed

+27
-4
lines changed

7 files changed

+27
-4
lines changed

doc/source/reference/general_functions.rst

+7
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ Top-level evaluation
7373

7474
eval
7575

76+
Datetime formats
77+
~~~~~~~~~~~~~~~~
78+
.. autosummary::
79+
:toctree: api/
80+
81+
tseries.api.guess_datetime_format
82+
7683
Hashing
7784
~~~~~~~
7885
.. autosummary::

doc/source/whatsnew/v2.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ Other enhancements
7878
- :attr:`Series.attrs` / :attr:`DataFrame.attrs` now uses a deepcopy for propagating ``attrs`` (:issue:`54134`).
7979
- :func:`read_csv` now supports ``on_bad_lines`` parameter with ``engine="pyarrow"``. (:issue:`54480`)
8080
- :func:`read_spss` now returns a :class:`DataFrame` that stores the metadata in :attr:`DataFrame.attrs`. (:issue:`54264`)
81+
- :func:`tseries.api.guess_datetime_format` is now part of the public API (:issue:`54727`)
8182
- :meth:`ExtensionArray._explode` interface method added to allow extension type implementations of the ``explode`` method (:issue:`54833`)
8283
- :meth:`ExtensionArray.duplicated` added to allow extension type implementations of the ``duplicated`` method (:issue:`55255`)
8384
- DataFrame.apply now allows the usage of numba (via ``engine="numba"``) to JIT compile the passed function, allowing for potential speedups (:issue:`54666`)

pandas/_libs/tslibs/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"is_supported_unit",
3434
"npy_unit_to_abbrev",
3535
"get_supported_reso",
36+
"guess_datetime_format",
3637
]
3738

3839
from pandas._libs.tslibs import dtypes # pylint: disable=import-self
@@ -63,6 +64,7 @@
6364
Tick,
6465
to_offset,
6566
)
67+
from pandas._libs.tslibs.parsing import guess_datetime_format
6668
from pandas._libs.tslibs.period import (
6769
IncompatibleFrequency,
6870
Period,

pandas/_libs/tslibs/parsing.pyi

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def try_parse_dates(
2424
parser,
2525
) -> npt.NDArray[np.object_]: ...
2626
def guess_datetime_format(
27-
dt_str,
27+
dt_str: str,
2828
dayfirst: bool | None = ...,
2929
) -> str | None: ...
3030
def concat_date_cols(

pandas/_libs/tslibs/parsing.pyx

+12-2
Original file line numberDiff line numberDiff line change
@@ -855,14 +855,24 @@ def guess_datetime_format(dt_str: str, bint dayfirst=False) -> str | None:
855855
Datetime string to guess the format of.
856856
dayfirst : bool, default False
857857
If True parses dates with the day first, eg 20/01/2005
858-
Warning: dayfirst=True is not strict, but will prefer to parse
859-
with day first (this is a known bug).
858+
859+
.. warning::
860+
dayfirst=True is not strict, but will prefer to parse
861+
with day first (this is a known bug).
860862

861863
Returns
862864
-------
863865
str or None : ret
864866
datetime format string (for `strftime` or `strptime`),
865867
or None if it can't be guessed.
868+
869+
Examples
870+
--------
871+
>>> from pandas.tseries.api import guess_datetime_format
872+
>>> guess_datetime_format('09/13/2023')
873+
'%m/%d/%Y'
874+
875+
>>> guess_datetime_format('2023|September|13')
866876
"""
867877
cdef:
868878
NPY_DATETIMEUNIT out_bestunit

pandas/tests/tslibs/test_api.py

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def test_namespace():
5757
"is_supported_unit",
5858
"get_supported_reso",
5959
"npy_unit_to_abbrev",
60+
"guess_datetime_format",
6061
]
6162

6263
expected = set(submodules + api)

pandas/tseries/api.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
Timeseries API
33
"""
44

5+
from pandas._libs.tslibs.parsing import guess_datetime_format
6+
57
from pandas.tseries import offsets
68
from pandas.tseries.frequencies import infer_freq
79

8-
__all__ = ["infer_freq", "offsets"]
10+
__all__ = ["infer_freq", "offsets", "guess_datetime_format"]

0 commit comments

Comments
 (0)