Skip to content

Commit 7719aaa

Browse files
h-vishalsimonjayhawkins
authored andcommitted
Backport PR pandas-dev#32479 on branch 1.0.x (BUG: Fix issue with datetime[ns, tz] input in Block.setitem)
1 parent bfe70aa commit 7719aaa

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

doc/source/whatsnew/v1.0.4.rst

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Fixed regressions
2828
- Bug in :meth:`Series.groupby` would raise ``ValueError`` when grouping by :class:`PeriodIndex` level (:issue:`34010`)
2929
- Bug in :meth:`GroupBy.rolling.apply` ignores args and kwargs parameters (:issue:`33433`)
3030
- More informative error message with ``np.min`` or ``np.max`` on unordered :class:`Categorical` (:issue:`33115`)
31+
- Fixed regression in :meth:`DataFrame.loc` and :meth:`Series.loc` throwing an error when a ``datetime64[ns, tz]`` value is provided (:issue:`32395`)
3132
-
3233

3334
.. _whatsnew_104.bug_fixes:

pandas/core/internals/blocks.py

+7
Original file line numberDiff line numberDiff line change
@@ -856,8 +856,10 @@ def setitem(self, indexer, value):
856856
if is_extension_array_dtype(getattr(value, "dtype", None)):
857857
# We need to be careful not to allow through strings that
858858
# can be parsed to EADtypes
859+
is_ea_value = True
859860
arr_value = value
860861
else:
862+
is_ea_value = False
861863
arr_value = np.array(value)
862864

863865
# cast the values to a type that can hold nan (if necessary)
@@ -894,6 +896,11 @@ def setitem(self, indexer, value):
894896
values[indexer] = value
895897
return self.make_block(Categorical(self.values, dtype=arr_value.dtype))
896898

899+
elif exact_match and is_ea_value:
900+
# GH#32395 if we're going to replace the values entirely, just
901+
# substitute in the new array
902+
return self.make_block(arr_value)
903+
897904
# if we are an exact match (ex-broadcasting),
898905
# then use the resultant dtype
899906
elif exact_match:

pandas/tests/extension/base/setitem.py

+29
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55

66
import pandas as pd
7+
import pandas._testing as tm
78
from pandas.core.arrays.numpy_ import PandasDtype
89

910
from .base import BaseExtensionTests
@@ -314,3 +315,31 @@ def test_setitem_nullable_mask(self, data):
314315
mask = pd.array([True, True, True, False, False])
315316
arr[mask] = data[0]
316317
self.assert_extension_array_equal(expected, arr)
318+
319+
def test_setitem_dataframe_column_with_index(self, data):
320+
# https://github.com/pandas-dev/pandas/issues/32395
321+
df = expected = pd.DataFrame({"data": pd.Series(data)})
322+
result = pd.DataFrame(index=df.index)
323+
result.loc[df.index, "data"] = df["data"]
324+
self.assert_frame_equal(result, expected)
325+
326+
def test_setitem_dataframe_column_without_index(self, data):
327+
# https://github.com/pandas-dev/pandas/issues/32395
328+
df = expected = pd.DataFrame({"data": pd.Series(data)})
329+
result = pd.DataFrame(index=df.index)
330+
result.loc[:, "data"] = df["data"]
331+
self.assert_frame_equal(result, expected)
332+
333+
def test_setitem_series_with_index(self, data):
334+
# https://github.com/pandas-dev/pandas/issues/32395
335+
ser = expected = pd.Series(data, name="data")
336+
result = pd.Series(index=ser.index, dtype=np.object, name="data")
337+
result.loc[ser.index] = ser
338+
self.assert_series_equal(result, expected)
339+
340+
def test_setitem_series_without_index(self, data):
341+
# https://github.com/pandas-dev/pandas/issues/32395
342+
ser = expected = pd.Series(data, name="data")
343+
result = pd.Series(index=ser.index, dtype=np.object, name="data")
344+
result.loc[:] = ser
345+
self.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)