Skip to content

Backport PR #32479 on branch 1.0.x (BUG: Fix issue with datetime[ns, tz] input in Block.setitem) #34369

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

Merged
merged 2 commits into from
May 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Fixed regressions
- Bug in :meth:`Series.groupby` would raise ``ValueError`` when grouping by :class:`PeriodIndex` level (:issue:`34010`)
- Bug in :meth:`GroupBy.rolling.apply` ignores args and kwargs parameters (:issue:`33433`)
- More informative error message with ``np.min`` or ``np.max`` on unordered :class:`Categorical` (:issue:`33115`)
- Fixed regression in :meth:`DataFrame.loc` and :meth:`Series.loc` throwing an error when a ``datetime64[ns, tz]`` value is provided (:issue:`32395`)
-

.. _whatsnew_104.bug_fixes:
Expand Down
7 changes: 7 additions & 0 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,13 @@ def shape(self) -> Tuple[int, ...]:
"""
return (len(self),)

@property
def size(self) -> int:
"""
The number of elements in the array.
"""
return np.prod(self.shape)

@property
def ndim(self) -> int:
"""
Expand Down
7 changes: 7 additions & 0 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -856,8 +856,10 @@ def setitem(self, indexer, value):
if is_extension_array_dtype(getattr(value, "dtype", None)):
# We need to be careful not to allow through strings that
# can be parsed to EADtypes
is_ea_value = True
arr_value = value
else:
is_ea_value = False
arr_value = np.array(value)

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

elif exact_match and is_ea_value:
# GH#32395 if we're going to replace the values entirely, just
# substitute in the new array
return self.make_block(arr_value)

# if we are an exact match (ex-broadcasting),
# then use the resultant dtype
elif exact_match:
Expand Down
3 changes: 3 additions & 0 deletions pandas/tests/extension/base/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class BaseInterfaceTests(BaseExtensionTests):
def test_len(self, data):
assert len(data) == 100

def test_size(self, data):
assert data.size == 100

def test_ndim(self, data):
assert data.ndim == 1

Expand Down
29 changes: 29 additions & 0 deletions pandas/tests/extension/base/setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pytest

import pandas as pd
import pandas._testing as tm
from pandas.core.arrays.numpy_ import PandasDtype

from .base import BaseExtensionTests
Expand Down Expand Up @@ -314,3 +315,31 @@ def test_setitem_nullable_mask(self, data):
mask = pd.array([True, True, True, False, False])
arr[mask] = data[0]
self.assert_extension_array_equal(expected, arr)

def test_setitem_dataframe_column_with_index(self, data):
# https://github.com/pandas-dev/pandas/issues/32395
df = expected = pd.DataFrame({"data": pd.Series(data)})
result = pd.DataFrame(index=df.index)
result.loc[df.index, "data"] = df["data"]
self.assert_frame_equal(result, expected)

def test_setitem_dataframe_column_without_index(self, data):
# https://github.com/pandas-dev/pandas/issues/32395
df = expected = pd.DataFrame({"data": pd.Series(data)})
result = pd.DataFrame(index=df.index)
result.loc[:, "data"] = df["data"]
self.assert_frame_equal(result, expected)

def test_setitem_series_with_index(self, data):
# https://github.com/pandas-dev/pandas/issues/32395
ser = expected = pd.Series(data, name="data")
result = pd.Series(index=ser.index, dtype=np.object, name="data")
result.loc[ser.index] = ser
self.assert_series_equal(result, expected)

def test_setitem_series_without_index(self, data):
# https://github.com/pandas-dev/pandas/issues/32395
ser = expected = pd.Series(data, name="data")
result = pd.Series(index=ser.index, dtype=np.object, name="data")
result.loc[:] = ser
self.assert_series_equal(result, expected)