Skip to content

CLN: Prelims for stronger typing in Block methods #32712

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 1 commit into from
Mar 16, 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
2 changes: 1 addition & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -6464,7 +6464,7 @@ def melt(
# ----------------------------------------------------------------------
# Time series-related

def diff(self, periods=1, axis=0) -> "DataFrame":
def diff(self, periods: int = 1, axis: Axis = 0) -> "DataFrame":
"""
First discrete difference of element.

Expand Down
5 changes: 4 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8587,12 +8587,15 @@ def _where(
for dt in cond.dtypes:
if not is_bool_dtype(dt):
raise ValueError(msg.format(dtype=dt))
else:
# GH#21947 we have an empty DataFrame, could be object-dtype
cond = cond.astype(bool)

cond = -cond if inplace else cond

# try to align with other
try_quick = True
if hasattr(other, "align"):
if isinstance(other, NDFrame):

# align with me
if other.ndim <= self.ndim:
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,8 +569,8 @@ def setitem(self, indexer, value) -> "BlockManager":
def putmask(self, **kwargs):
return self.apply("putmask", **kwargs)

def diff(self, **kwargs) -> "BlockManager":
return self.apply("diff", **kwargs)
def diff(self, n: int, axis: int) -> "BlockManager":
return self.apply("diff", n=n, axis=axis)

def interpolate(self, **kwargs) -> "BlockManager":
return self.apply("interpolate", **kwargs)
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,7 @@ def __getitem__(self, key):

if com.is_bool_indexer(key):
key = check_bool_indexer(self.index, key)
key = np.asarray(key, dtype=bool)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn’t check_bool_indexer guarantee this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i was surprised by this too, but no

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, let's investigate if we can do this inside check_bool_indexer (either open an issue or do as a followon)

return self._get_values(key)

return self._get_with(key)
Expand Down Expand Up @@ -993,6 +994,7 @@ def __setitem__(self, key, value):

if com.is_bool_indexer(key):
key = check_bool_indexer(self.index, key)
key = np.asarray(key, dtype=bool)
try:
self._where(~key, value, inplace=True)
return
Expand Down Expand Up @@ -2240,7 +2242,7 @@ def cov(self, other, min_periods=None) -> float:
return np.nan
return nanops.nancov(this.values, other.values, min_periods=min_periods)

def diff(self, periods=1) -> "Series":
def diff(self, periods: int = 1) -> "Series":
"""
First discrete difference of element.

Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/frame/indexing/test_where.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,8 @@ def test_where_none(self):
def test_where_empty_df_and_empty_cond_having_non_bool_dtypes(self):
# see gh-21947
df = pd.DataFrame(columns=["a"])
cond = df.applymap(lambda x: x > 0)
cond = df
assert (cond.dtypes == object).all()

result = df.where(cond)
tm.assert_frame_equal(result, df)
Expand Down