Skip to content
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

ENH: Support for list of formats in pd.to_datetime() (pandas-dev#55226) #55698

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
255cef6
#Issue55226
gusthavoMFS Oct 26, 2023
2ebdff2
Merge branch 'main' of https://github.com/pandas-dev/pandas
gusthavoMFS Oct 26, 2023
60d3366
ENH: Support for list of formats in pd.to_datetime() (panda-ver#55226)
gusthavoMFS Oct 26, 2023
12647b4
ENH: Support for list of formats in pd.to_datetime() (pandas-dev#55226)
gusthavoMFS Oct 26, 2023
f343616
ENH: Support for list of formats in pd.to_datetime() (pandas-dev#55226)
gusthavoMFS Oct 26, 2023
ce9c526
ENH: Support for list of formats in pd.to_datetime() (pandas-dev#55226)
gusthavoMFS Oct 26, 2023
049f3d3
ENH: Support for list of formats in pd.to_datetime() (pandas-dev#5522…
gusthavoMFS Oct 26, 2023
6ce0c74
ENH: Support for list of formats in pd.to_datetime() (pandas-dev#5522…
gusthavoMFS Oct 26, 2023
95b616a
Teste
gusthavoMFS Oct 26, 2023
7786be4
ENH: Support for list of formats in pd.to_datetime() (pandas-dev#5522…
gusthavoMFS Oct 26, 2023
112fa91
alterations
gusthavoMFS Nov 15, 2023
6cd846d
new alterations
gusthavoMFS Nov 16, 2023
135a826
alterations2
gusthavoMFS Nov 18, 2023
be1a212
alterations3
gusthavoMFS Nov 18, 2023
b3bfed5
alterations4
gusthavoMFS Nov 18, 2023
91f72a0
trim trailing whitespaces
leogiuris Nov 20, 2023
fffd340
ENH: Support for list of formats in pd.to_datetime() (pandas-dev#55226)
gusthavoMFS Nov 21, 2023
2ffda59
Merge branch 'main' of https://github.com/gusthavoMFS/pandas
gusthavoMFS Nov 21, 2023
ae8534f
ENH: Support for list of formats in pd.to_datetime() (pandas-dev#55226)
gusthavoMFS Nov 21, 2023
dc1271a
Revert "alterations3"
leogiuris Nov 24, 2023
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
26 changes: 21 additions & 5 deletions pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
Callable,
TypedDict,
Union,
List,
cast,
overload,
)
Expand Down Expand Up @@ -399,6 +400,8 @@ def _convert_listlike_datetimes(
-------
Index-like of parsed dates
"""


if isinstance(arg, (list, tuple)):
arg = np.array(arg, dtype="O")
elif isinstance(arg, NumpyExtensionArray):
Expand Down Expand Up @@ -715,13 +718,14 @@ def to_datetime(
...


#MUDANDO AQUI
Copy link
Member

Choose a reason for hiding this comment

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

e.g. this

def to_datetime(
arg: DatetimeScalarOrArrayConvertible | DictConvertible,
errors: DateTimeErrorChoices = "raise",
dayfirst: bool = False,
yearfirst: bool = False,
utc: bool = False,
format: str | None = None,
format: str | List[str] | None = None,
exact: bool | lib.NoDefault = lib.no_default,
unit: str | None = None,
infer_datetime_format: lib.NoDefault | bool = lib.no_default,
Expand Down Expand Up @@ -795,7 +799,7 @@ def to_datetime(
<https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html
#time-zone-handling>`_.

format : str, default None
format : str, List[str] , default None
The strftime to parse time, e.g. :const:`"%d/%m/%Y"`. See
`strftime documentation
<https://docs.python.org/3/library/datetime.html
Expand Down Expand Up @@ -1065,7 +1069,9 @@ def to_datetime(
DatetimeIndex(['2018-10-26 12:00:00+00:00', '2020-01-01 18:00:00+00:00'],
dtype='datetime64[ns, UTC]', freq=None)
"""
if exact is not lib.no_default and format in {"mixed", "ISO8601"}:


if exact is not lib.no_default and format in {"mixed","ISO8601"}:
raise ValueError("Cannot use 'exact' when 'format' is 'mixed' or 'ISO8601'")
if infer_datetime_format is not lib.no_default:
warnings.warn(
Expand Down Expand Up @@ -1094,6 +1100,8 @@ def to_datetime(
# pylint: disable-next=used-before-assignment
result: Timestamp | NaTType | Series | Index



if isinstance(arg, Timestamp):
result = arg
if utc:
Expand Down Expand Up @@ -1125,7 +1133,7 @@ def to_datetime(
argc = cast(
Union[list, tuple, ExtensionArray, np.ndarray, "Series", Index], arg
)
cache_array = _maybe_cache(argc, format, cache, convert_listlike)
cache_array = _maybe_cache(argc, format, cache, convert_listlike) # VER TAMBÉM AQUI
except OutOfBoundsDatetime:
# caching attempts to create a DatetimeIndex, which may raise
# an OOB. If that's the desired behavior, then just reraise...
Expand All @@ -1138,7 +1146,15 @@ def to_datetime(
if not cache_array.empty:
result = _convert_and_box_cache(argc, cache_array)
else:
result = convert_listlike(argc, format)
#CHANGED HERE
if isinstance(format, (list, tuple)):
format = np.array(format, dtype="O")
# return list
return_list = [Timestamp(convert_listlike([argc[i]],fmt)[0]) for i,fmt in enumerate(format)]
# transformed object in DatetimeIndex
result = DatetimeIndex(return_list)
else:
result = convert_listlike(argc, format)
else:
result = convert_listlike(np.array([arg]), format)[0]
if isinstance(arg, bool) and isinstance(result, np.bool_):
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/tools/test_to_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,19 @@ def test_to_datetime_now_with_format(self, format, expected_ds, string, attribut
)
assert (expected - result).max().total_seconds() < 1


def test_to_datetime_where_formart_is_list(self):
# https://github.com/pandas-dev/pandas/issues/55226
data = ['2023-10-12','2023-10-13 14:30:00']
all_formats = ['%Y-%m-%d','%Y-%m-%d %H:%M:%S']
result = to_datetime(data,format=all_formats)
expected = DatetimeIndex(
['2023-10-12 00:00:00','2023-10-13 14:30:00'], dtype="datetime64[ns]",
freq=None
)
tm.assert_index_equal(result,expected)


@pytest.mark.parametrize(
"dt", [np.datetime64("2000-01-01"), np.datetime64("2000-01-02")]
)
Expand Down
Loading