-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Do not fail when parsing pydatetime objects in pd.to_datetime #49893
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
Changes from 11 commits
82f3252
2008eb3
560a02a
79cdad4
f38a7cd
5884f4e
6b58618
e665ada
af272e1
12f5ac7
06f1a53
ab661cf
ec27d42
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -469,6 +469,88 @@ def test_to_datetime_mixed_datetime_and_string(self): | |
expected = to_datetime([d1, d2]).tz_convert(pytz.FixedOffset(-60)) | ||
tm.assert_index_equal(res, expected) | ||
|
||
@pytest.mark.parametrize( | ||
"fmt", | ||
["%Y-%d-%m %H:%M:%S%z", "%Y-%m-%d %H:%M:%S%z"], | ||
ids=["non-ISO8601 format", "ISO8601 format"], | ||
) | ||
@pytest.mark.parametrize( | ||
"utc, input, expected", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe avoid using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good one, thanks! |
||
[ | ||
pytest.param( | ||
True, | ||
["2000-01-01 01:00:00-08:00", "2000-01-01 02:00:00-08:00"], | ||
DatetimeIndex( | ||
["2000-01-01 09:00:00+00:00", "2000-01-01 10:00:00+00:00"], | ||
dtype="datetime64[ns, UTC]", | ||
), | ||
id="all tz-aware, with utc", | ||
), | ||
pytest.param( | ||
False, | ||
["2000-01-01 01:00:00+00:00", "2000-01-01 02:00:00+00:00"], | ||
DatetimeIndex( | ||
["2000-01-01 01:00:00+00:00", "2000-01-01 02:00:00+00:00"], | ||
), | ||
id="all tz-aware, without utc", | ||
), | ||
pytest.param( | ||
True, | ||
["2000-01-01 01:00:00-08:00", "2000-01-01 02:00:00+00:00"], | ||
DatetimeIndex( | ||
["2000-01-01 09:00:00+00:00", "2000-01-01 02:00:00+00:00"], | ||
dtype="datetime64[ns, UTC]", | ||
), | ||
id="all tz-aware, mixed offsets, with utc", | ||
), | ||
], | ||
) | ||
@pytest.mark.parametrize( | ||
"constructor", | ||
[Timestamp, lambda x: Timestamp(x).to_pydatetime()], | ||
mroeschke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
def test_to_datetime_mixed_datetime_and_string_with_format( | ||
self, fmt, utc, input, expected, constructor | ||
): | ||
# https://github.com/pandas-dev/pandas/issues/49298 | ||
# note: ISO8601 formats go down a fastpath, so we need to check both | ||
# a ISO8601 format and a non-ISO8601 one | ||
ts1 = constructor(input[0]) | ||
ts2 = input[1] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this supposed to be wrapped by the constructor as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for you review! I didn't intend it to be - like this the input contains a mixture of strings and pydatetime objects |
||
result = to_datetime([ts1, ts2], format=fmt, utc=utc) | ||
tm.assert_index_equal(result, expected) | ||
|
||
@pytest.mark.parametrize( | ||
"fmt", | ||
["%Y-%d-%m %H:%M:%S%z", "%Y-%m-%d %H:%M:%S%z"], | ||
ids=["non-ISO8601 format", "ISO8601 format"], | ||
) | ||
@pytest.mark.parametrize( | ||
"input", | ||
[ | ||
pytest.param( | ||
["2000-01-01 01:00:00-08:00", "2000-01-01 02:00:00-07:00"], | ||
id="all tz-aware, mixed timezones, without utc", | ||
), | ||
], | ||
) | ||
@pytest.mark.parametrize( | ||
"constructor", | ||
[Timestamp, lambda x: Timestamp(x).to_pydatetime()], | ||
) | ||
def test_to_datetime_mixed_datetime_and_string_with_format_raises( | ||
self, fmt, input, constructor | ||
): | ||
# https://github.com/pandas-dev/pandas/issues/49298 | ||
# note: ISO8601 formats go down a fastpath, so we need to check both | ||
# a ISO8601 format and a non-ISO8601 one | ||
ts1 = constructor(input[0]) | ||
ts2 = constructor(input[1]) | ||
with pytest.raises( | ||
ValueError, match="cannot be converted to datetime64 unless utc=True" | ||
): | ||
to_datetime([ts1, ts2], format=fmt, utc=False) | ||
|
||
@pytest.mark.parametrize("infer_datetime_format", [True, False]) | ||
def test_to_datetime_np_str(self, infer_datetime_format): | ||
# GH#32264 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is the difference here for if all the strings are tznaive but we saw a tzaware datetime object?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's right