-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: Series.where casting dt64 to int64 #38073
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
Changes from all commits
24dd5dd
ac47fab
18f1671
95d7aa3
8862b0e
eb686bf
e0fcbe5
48e99c4
75ce3cd
5197e96
0d0165c
f9786c6
1c04797
d797c21
d9f1e3a
08c06e9
4cef5ea
1e70e20
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 |
---|---|---|
|
@@ -1332,6 +1332,22 @@ def shift(self, periods: int, axis: int = 0, fill_value=None): | |
|
||
return [self.make_block(new_values)] | ||
|
||
def _maybe_reshape_where_args(self, values, other, cond, axis): | ||
transpose = self.ndim == 2 | ||
|
||
cond = _extract_bool_array(cond) | ||
|
||
# If the default broadcasting would go in the wrong direction, then | ||
# explicitly reshape other instead | ||
if getattr(other, "ndim", 0) >= 1: | ||
if values.ndim - 1 == other.ndim and axis == 1: | ||
other = other.reshape(tuple(other.shape + (1,))) | ||
elif transpose and values.ndim == self.ndim - 1: | ||
# TODO(EA2D): not neceesssary with 2D EAs | ||
cond = cond.T | ||
|
||
return other, cond | ||
|
||
def where( | ||
self, other, cond, errors="raise", try_cast: bool = False, axis: int = 0 | ||
) -> List["Block"]: | ||
|
@@ -1354,7 +1370,6 @@ def where( | |
""" | ||
import pandas.core.computation.expressions as expressions | ||
|
||
cond = _extract_bool_array(cond) | ||
assert not isinstance(other, (ABCIndex, ABCSeries, ABCDataFrame)) | ||
|
||
assert errors in ["raise", "ignore"] | ||
|
@@ -1365,17 +1380,7 @@ def where( | |
if transpose: | ||
values = values.T | ||
|
||
# If the default broadcasting would go in the wrong direction, then | ||
# explicitly reshape other instead | ||
if getattr(other, "ndim", 0) >= 1: | ||
if values.ndim - 1 == other.ndim and axis == 1: | ||
other = other.reshape(tuple(other.shape + (1,))) | ||
elif transpose and values.ndim == self.ndim - 1: | ||
# TODO(EA2D): not neceesssary with 2D EAs | ||
cond = cond.T | ||
|
||
if not hasattr(cond, "shape"): | ||
raise ValueError("where must have a condition that is ndarray like") | ||
other, cond = self._maybe_reshape_where_args(values, other, cond, axis) | ||
|
||
if cond.ravel("K").all(): | ||
result = values | ||
|
@@ -2128,6 +2133,26 @@ def to_native_types(self, na_rep="NaT", **kwargs): | |
result = arr._format_native_types(na_rep=na_rep, **kwargs) | ||
return self.make_block(result) | ||
|
||
def where( | ||
self, other, cond, errors="raise", try_cast: bool = False, axis: int = 0 | ||
) -> List["Block"]: | ||
# TODO(EA2D): reshape unnecessary with 2D EAs | ||
arr = self.array_values().reshape(self.shape) | ||
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. you can almost use your ravel_compat decorator |
||
|
||
other, cond = self._maybe_reshape_where_args(arr, other, cond, axis) | ||
|
||
try: | ||
res_values = arr.T.where(cond, other).T | ||
except (ValueError, TypeError): | ||
return super().where( | ||
other, cond, errors=errors, try_cast=try_cast, axis=axis | ||
) | ||
|
||
# TODO(EA2D): reshape not needed with 2D EAs | ||
res_values = res_values.reshape(self.values.shape) | ||
nb = self.make_block_same_class(res_values) | ||
return [nb] | ||
|
||
def _can_hold_element(self, element: Any) -> bool: | ||
arr = self.array_values() | ||
|
||
|
@@ -2196,6 +2221,7 @@ class DatetimeTZBlock(ExtensionBlock, DatetimeBlock): | |
fillna = DatetimeBlock.fillna # i.e. Block.fillna | ||
fill_value = DatetimeBlock.fill_value | ||
_can_hold_na = DatetimeBlock._can_hold_na | ||
where = DatetimeBlock.where | ||
|
||
array_values = ExtensionBlock.array_values | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -278,7 +278,7 @@ def test_array_inference_fails(data): | |
tm.assert_extension_array_equal(result, expected) | ||
|
||
|
||
@pytest.mark.parametrize("data", [np.array([[1, 2], [3, 4]]), [[1, 2], [3, 4]]]) | ||
@pytest.mark.parametrize("data", [np.array(0)]) | ||
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. we pass thru 2-D pandas arrays? 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. DTA/TDA._validate_listlike calls pd.array on possibly-2D inputs, which go through PandasArray |
||
def test_nd_raises(data): | ||
with pytest.raises(ValueError, match="PandasArray must be 1-dimensional"): | ||
pd.array(data, dtype="int64") | ||
|
Uh oh!
There was an error while loading. Please reload this page.