-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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
BUG: mixed-type mixed-timezone/awareness #55793
Changes from 5 commits
0cea20c
7396939
30cbf19
79ef23e
60ec374
57c8bae
0f2b27c
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 |
---|---|---|
|
@@ -14,6 +14,8 @@ cdef class DatetimeParseState: | |
cdef: | ||
bint found_tz | ||
bint found_naive | ||
bint found_naive_str | ||
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 it easy to add comments inline with these members or as part of the class? I can see the distinction between 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. will update with comment(s). we'll be able to simplify this a ton in 3.0 once deprecation are enforced |
||
bint found_other | ||
bint creso_ever_changed | ||
NPY_DATETIMEUNIT creso | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3723,3 +3723,142 @@ def test_to_datetime_with_empty_str_utc_false_offsets_and_format_mixed(): | |
to_datetime( | ||
["2020-01-01 00:00+00:00", "2020-01-01 00:00+02:00", ""], format="mixed" | ||
) | ||
|
||
|
||
def test_to_datetime_mixed_tzs_mixed_types(): | ||
# GH#55793, GH#55693 mismatched tzs but one is str and other is | ||
# datetime object | ||
ts = Timestamp("2016-01-02 03:04:05", tz="US/Pacific") | ||
dtstr = "2023-10-30 15:06+01" | ||
arr = [ts, dtstr] | ||
|
||
msg = ( | ||
"Mixed timezones detected. pass utc=True in to_datetime or tz='UTC' " | ||
"in DatetimeIndex to convert to a common timezone" | ||
) | ||
with pytest.raises(ValueError, match=msg): | ||
to_datetime(arr) | ||
with pytest.raises(ValueError, match=msg): | ||
to_datetime(arr, format="mixed") | ||
with pytest.raises(ValueError, match=msg): | ||
DatetimeIndex(arr) | ||
|
||
|
||
def test_to_datetime_mixed_types_matching_tzs(): | ||
# GH#55793 | ||
dtstr = "2023-11-01 09:22:03-07:00" | ||
ts = Timestamp(dtstr) | ||
arr = [ts, dtstr] | ||
res1 = to_datetime(arr) | ||
res2 = to_datetime(arr[::-1])[::-1] | ||
res3 = to_datetime(arr, format="mixed") | ||
res4 = DatetimeIndex(arr) | ||
|
||
expected = DatetimeIndex([ts, ts]) | ||
tm.assert_index_equal(res1, expected) | ||
tm.assert_index_equal(res2, expected) | ||
tm.assert_index_equal(res3, expected) | ||
tm.assert_index_equal(res4, expected) | ||
|
||
|
||
dtstr = "2020-01-01 00:00+00:00" | ||
ts = Timestamp(dtstr) | ||
|
||
|
||
@pytest.mark.filterwarnings("ignore:Could not infer format:UserWarning") | ||
@pytest.mark.parametrize( | ||
"aware_val", | ||
[dtstr, Timestamp(dtstr)], | ||
ids=lambda x: type(x).__name__, | ||
) | ||
@pytest.mark.parametrize( | ||
"naive_val", | ||
[dtstr[:-6], ts.tz_localize(None), ts.date(), ts.asm8, ts.value, float(ts.value)], | ||
ids=lambda x: type(x).__name__, | ||
) | ||
@pytest.mark.parametrize("naive_first", [True, False]) | ||
def test_to_datetime_mixed_awareness_mixed_types(aware_val, naive_val, naive_first): | ||
# GH#55793, GH#55693 | ||
# Empty string parses to NaT | ||
vals = [aware_val, naive_val, ""] | ||
|
||
vec = vals | ||
if naive_first: | ||
# alas, the behavior is order-dependent, so we test both ways | ||
vec = [naive_val, aware_val, ""] | ||
|
||
# both_strs-> paths that were previously already deprecated with warning | ||
# issued in _array_to_datetime_object | ||
both_strs = isinstance(aware_val, str) and isinstance(naive_val, str) | ||
isinstance(aware_val, str) or isinstance(naive_val, str) | ||
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. what's this statement doing? 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. probably left over from a previous commit, will remove |
||
has_numeric = isinstance(naive_val, (int, float)) | ||
|
||
depr_msg = "In a future version of pandas, parsing datetimes with mixed time zones" | ||
|
||
first_non_null = next(x for x in vec if x != "") | ||
# if first_non_null is a not a string, _guess_datetime_format_for_array | ||
# doesn't guess a format so we don't go through array_strptime | ||
if not isinstance(first_non_null, str): | ||
# that case goes through array_strptime which has different behavior | ||
msg = "Cannot mix tz-aware with tz-naive values" | ||
if naive_first and isinstance(aware_val, Timestamp): | ||
if isinstance(naive_val, Timestamp): | ||
msg = "Tz-aware datetime.datetime cannot be converted to datetime64" | ||
with pytest.raises(ValueError, match=msg): | ||
to_datetime(vec) | ||
else: | ||
with pytest.raises(ValueError, match=msg): | ||
to_datetime(vec) | ||
|
||
# No warning/error with utc=True | ||
to_datetime(vec, utc=True) | ||
|
||
elif has_numeric and vec.index(aware_val) < vec.index(naive_val): | ||
msg = "time data .* doesn't match format" | ||
with pytest.raises(ValueError, match=msg): | ||
to_datetime(vec) | ||
with pytest.raises(ValueError, match=msg): | ||
to_datetime(vec, utc=True) | ||
|
||
elif both_strs and vec.index(aware_val) < vec.index(naive_val): | ||
msg = r"time data \"2020-01-01 00:00\" doesn't match format" | ||
with pytest.raises(ValueError, match=msg): | ||
to_datetime(vec) | ||
with pytest.raises(ValueError, match=msg): | ||
to_datetime(vec, utc=True) | ||
|
||
elif both_strs and vec.index(naive_val) < vec.index(aware_val): | ||
msg = "unconverted data remains when parsing with format" | ||
with pytest.raises(ValueError, match=msg): | ||
to_datetime(vec) | ||
with pytest.raises(ValueError, match=msg): | ||
to_datetime(vec, utc=True) | ||
|
||
else: | ||
with tm.assert_produces_warning(FutureWarning, match=depr_msg): | ||
to_datetime(vec) | ||
|
||
# No warning/error with utc=True | ||
to_datetime(vec, utc=True) | ||
|
||
if both_strs: | ||
with tm.assert_produces_warning(FutureWarning, match=depr_msg): | ||
to_datetime(vec, format="mixed") | ||
with tm.assert_produces_warning(FutureWarning, match=depr_msg): | ||
msg = "DatetimeIndex has mixed timezones" | ||
with pytest.raises(TypeError, match=msg): | ||
DatetimeIndex(vec) | ||
else: | ||
msg = "Cannot mix tz-aware with tz-naive values" | ||
if naive_first and isinstance(aware_val, Timestamp): | ||
if isinstance(naive_val, Timestamp): | ||
msg = "Tz-aware datetime.datetime cannot be converted to datetime64" | ||
with pytest.raises(ValueError, match=msg): | ||
to_datetime(vec, format="mixed") | ||
with pytest.raises(ValueError, match=msg): | ||
DatetimeIndex(vec) | ||
else: | ||
with pytest.raises(ValueError, match=msg): | ||
to_datetime(vec, format="mixed") | ||
with pytest.raises(ValueError, match=msg): | ||
DatetimeIndex(vec) |
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.
Just to be clear this won't affect the
object
dtype case right? Maybe worth adding a test that is still possible if not already in the suiteThere 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.
as in,
pd.Index([datetime(2020, 1, 1, tzinfo=zoneinfo.ZoneInfo('US/Central')), datetime(2020, 1, 1, tzinfo=timezone.utc)])
? I don't think it should affect it, but yeah, a little test with this wouldn't hurtThere 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.
updated with test