Skip to content

Commit 4d507b0

Browse files
authored
BUG: partially-inferring pydatetime objects (#44296)
1 parent c6bdacb commit 4d507b0

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ Datetimelike
475475
- Bug in :meth:`date_range` and :meth:`bdate_range` do not return right bound when ``start`` = ``end`` and set is closed on one side (:issue:`43394`)
476476
- Bug in inplace addition and subtraction of :class:`DatetimeIndex` or :class:`TimedeltaIndex` with :class:`DatetimeArray` or :class:`TimedeltaArray` (:issue:`43904`)
477477
- Bug in in calling ``np.isnan``, ``np.isfinite``, or ``np.isinf`` on a timezone-aware :class:`DatetimeIndex` incorrectly raising ``TypeError`` (:issue:`43917`)
478+
- Bug in constructing a :class:`Series` from datetime-like strings with mixed timezones incorrectly partially-inferring datetime values (:issue:`40111`)
478479
-
479480

480481
Timedelta

pandas/core/dtypes/cast.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1526,7 +1526,7 @@ def try_datetime(v: np.ndarray) -> ArrayLike:
15261526
try:
15271527
# GH#19671 we pass require_iso8601 to be relatively strict
15281528
# when parsing strings.
1529-
dta = sequence_to_datetimes(v, require_iso8601=True, allow_object=True)
1529+
dta = sequence_to_datetimes(v, require_iso8601=True, allow_object=False)
15301530
except (ValueError, TypeError):
15311531
# e.g. <class 'numpy.timedelta64'> is not convertible to datetime
15321532
return v.reshape(shape)

pandas/tests/series/test_constructors.py

+12
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,18 @@ def test_constructor_with_datetime_tz2(self):
10451045
expected = Series(DatetimeIndex(["NaT", "NaT"], tz="US/Eastern"))
10461046
tm.assert_series_equal(s, expected)
10471047

1048+
def test_constructor_no_partial_datetime_casting(self):
1049+
# GH#40111
1050+
vals = [
1051+
"nan",
1052+
Timestamp("1990-01-01"),
1053+
"2015-03-14T16:15:14.123-08:00",
1054+
"2019-03-04T21:56:32.620-07:00",
1055+
None,
1056+
]
1057+
ser = Series(vals)
1058+
assert all(ser[i] is vals[i] for i in range(len(vals)))
1059+
10481060
@pytest.mark.parametrize("arr_dtype", [np.int64, np.float64])
10491061
@pytest.mark.parametrize("dtype", ["M8", "m8"])
10501062
@pytest.mark.parametrize("unit", ["ns", "us", "ms", "s", "h", "m", "D"])

pandas/tests/tools/test_to_datetime.py

+21-6
Original file line numberDiff line numberDiff line change
@@ -1123,17 +1123,32 @@ def test_iso8601_strings_mixed_offsets_with_naive(self):
11231123

11241124
def test_mixed_offsets_with_native_datetime_raises(self):
11251125
# GH 25978
1126-
ser = Series(
1126+
1127+
vals = [
1128+
"nan",
1129+
Timestamp("1990-01-01"),
1130+
"2015-03-14T16:15:14.123-08:00",
1131+
"2019-03-04T21:56:32.620-07:00",
1132+
None,
1133+
]
1134+
ser = Series(vals)
1135+
assert all(ser[i] is vals[i] for i in range(len(vals))) # GH#40111
1136+
1137+
mixed = to_datetime(ser)
1138+
expected = Series(
11271139
[
1128-
"nan",
1140+
"NaT",
11291141
Timestamp("1990-01-01"),
1130-
"2015-03-14T16:15:14.123-08:00",
1131-
"2019-03-04T21:56:32.620-07:00",
1142+
Timestamp("2015-03-14T16:15:14.123-08:00").to_pydatetime(),
1143+
Timestamp("2019-03-04T21:56:32.620-07:00").to_pydatetime(),
11321144
None,
1133-
]
1145+
],
1146+
dtype=object,
11341147
)
1148+
tm.assert_series_equal(mixed, expected)
1149+
11351150
with pytest.raises(ValueError, match="Tz-aware datetime.datetime"):
1136-
to_datetime(ser)
1151+
to_datetime(mixed)
11371152

11381153
def test_non_iso_strings_with_tz_offset(self):
11391154
result = to_datetime(["March 1, 2018 12:00:00+0400"] * 2)

0 commit comments

Comments
 (0)