Skip to content

Commit 0f2ca37

Browse files
authored
BUG: Fix issue with datetime[ns, tz] input in Block.setitem GH32395 (#32479)
1 parent bad52a9 commit 0f2ca37

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,7 @@ Indexing
732732
- Bug in :meth:`DataFrame.iloc.__setitem__` creating a new array instead of overwriting ``Categorical`` values in-place (:issue:`32831`)
733733
- Bug in :class:`Interval` where a :class:`Timedelta` could not be added or subtracted from a :class:`Timestamp` interval (:issue:`32023`)
734734
- Bug in :meth:`DataFrame.copy` _item_cache not invalidated after copy causes post-copy value updates to not be reflected (:issue:`31784`)
735+
- Fixed regression in :meth:`DataFrame.loc` and :meth:`Series.loc` throwing an error when a ``datetime64[ns, tz]`` value is provided (:issue:`32395`)
735736
- Bug in `Series.__getitem__` with an integer key and a :class:`MultiIndex` with leading integer level failing to raise ``KeyError`` if the key is not present in the first level (:issue:`33355`)
736737
- Bug in :meth:`DataFrame.iloc` when slicing a single column-:class:`DataFrame`` with ``ExtensionDtype`` (e.g. ``df.iloc[:, :1]``) returning an invalid result (:issue:`32957`)
737738
- Bug in :meth:`DatetimeIndex.insert` and :meth:`TimedeltaIndex.insert` causing index ``freq`` to be lost when setting an element into an empty :class:`Series` (:issue:33573`)

pandas/core/internals/blocks.py

+7
Original file line numberDiff line numberDiff line change
@@ -824,8 +824,10 @@ def setitem(self, indexer, value):
824824
if is_extension_array_dtype(getattr(value, "dtype", None)):
825825
# We need to be careful not to allow through strings that
826826
# can be parsed to EADtypes
827+
is_ea_value = True
827828
arr_value = value
828829
else:
830+
is_ea_value = False
829831
arr_value = np.array(value)
830832

831833
if transpose:
@@ -853,6 +855,11 @@ def setitem(self, indexer, value):
853855
values[indexer] = value
854856
return self.make_block(Categorical(self.values, dtype=arr_value.dtype))
855857

858+
elif exact_match and is_ea_value:
859+
# GH#32395 if we're going to replace the values entirely, just
860+
# substitute in the new array
861+
return self.make_block(arr_value)
862+
856863
# if we are an exact match (ex-broadcasting),
857864
# then use the resultant dtype
858865
elif exact_match:

pandas/tests/extension/base/setitem.py

+28
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,31 @@ def test_setitem_preserves_views(self, data):
301301
data[0] = data[1]
302302
assert view1[0] == data[1]
303303
assert view2[0] == data[1]
304+
305+
def test_setitem_dataframe_column_with_index(self, data):
306+
# https://github.com/pandas-dev/pandas/issues/32395
307+
df = expected = pd.DataFrame({"data": pd.Series(data)})
308+
result = pd.DataFrame(index=df.index)
309+
result.loc[df.index, "data"] = df["data"]
310+
self.assert_frame_equal(result, expected)
311+
312+
def test_setitem_dataframe_column_without_index(self, data):
313+
# https://github.com/pandas-dev/pandas/issues/32395
314+
df = expected = pd.DataFrame({"data": pd.Series(data)})
315+
result = pd.DataFrame(index=df.index)
316+
result.loc[:, "data"] = df["data"]
317+
self.assert_frame_equal(result, expected)
318+
319+
def test_setitem_series_with_index(self, data):
320+
# https://github.com/pandas-dev/pandas/issues/32395
321+
ser = expected = pd.Series(data, name="data")
322+
result = pd.Series(index=ser.index, dtype=np.object, name="data")
323+
result.loc[ser.index] = ser
324+
self.assert_series_equal(result, expected)
325+
326+
def test_setitem_series_without_index(self, data):
327+
# https://github.com/pandas-dev/pandas/issues/32395
328+
ser = expected = pd.Series(data, name="data")
329+
result = pd.Series(index=ser.index, dtype=np.object, name="data")
330+
result.loc[:] = ser
331+
self.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)