Skip to content

Commit 8efbe12

Browse files
jbrockmendeljreback
authored andcommitted
CLN: assorted cleanups (#27301)
1 parent 9240439 commit 8efbe12

File tree

7 files changed

+24
-29
lines changed

7 files changed

+24
-29
lines changed

pandas/core/computation/expressions.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212

1313
from pandas._config import get_option
1414

15+
from pandas._libs.lib import values_from_object
16+
1517
from pandas.core.dtypes.generic import ABCDataFrame
1618

17-
import pandas.core.common as com
1819
from pandas.core.computation.check import _NUMEXPR_INSTALLED
1920

2021
if _NUMEXPR_INSTALLED:
@@ -129,9 +130,7 @@ def _evaluate_numexpr(op, op_str, a, b, truediv=True, reversed=False, **eval_kwa
129130

130131
def _where_standard(cond, a, b):
131132
return np.where(
132-
com.values_from_object(cond),
133-
com.values_from_object(a),
134-
com.values_from_object(b),
133+
values_from_object(cond), values_from_object(a), values_from_object(b)
135134
)
136135

137136

pandas/core/internals/blocks.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -397,10 +397,6 @@ def fillna(self, value, limit=None, inplace=False, downcast=None):
397397
raise ValueError("Limit must be an integer")
398398
if limit < 1:
399399
raise ValueError("Limit must be greater than 0")
400-
if self.ndim > 2:
401-
raise NotImplementedError(
402-
"number of dimensions for 'fillna' is currently limited to 2"
403-
)
404400
mask[mask.cumsum(self.ndim - 1) > limit] = False
405401

406402
if not self._can_hold_na:
@@ -853,6 +849,8 @@ def setitem(self, indexer, value):
853849
`indexer` is a direct slice/positional indexer. `value` must
854850
be a compatible shape.
855851
"""
852+
transpose = self.ndim == 2
853+
856854
# coerce None values, if appropriate
857855
if value is None:
858856
if self.is_numeric:
@@ -901,8 +899,8 @@ def setitem(self, indexer, value):
901899
dtype, _ = maybe_promote(arr_value.dtype)
902900
values = values.astype(dtype)
903901

904-
transf = (lambda x: x.T) if self.ndim == 2 else (lambda x: x)
905-
values = transf(values)
902+
if transpose:
903+
values = values.T
906904

907905
# length checking
908906
check_setitem_lengths(indexer, value, values)
@@ -961,7 +959,9 @@ def _is_empty_indexer(indexer):
961959

962960
# coerce and try to infer the dtypes of the result
963961
values = self._try_coerce_and_cast_result(values, dtype)
964-
block = self.make_block(transf(values))
962+
if transpose:
963+
values = values.T
964+
block = self.make_block(values)
965965
return block
966966

967967
def putmask(self, mask, new, align=True, inplace=False, axis=0, transpose=False):

pandas/core/internals/managers.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,8 +583,9 @@ def astype(self, dtype, **kwargs):
583583
def convert(self, **kwargs):
584584
return self.apply("convert", **kwargs)
585585

586-
def replace(self, **kwargs):
587-
return self.apply("replace", **kwargs)
586+
def replace(self, value, **kwargs):
587+
assert np.ndim(value) == 0, value
588+
return self.apply("replace", value=value, **kwargs)
588589

589590
def replace_list(self, src_list, dest_list, inplace=False, regex=False):
590591
""" do a list replace """
@@ -617,6 +618,7 @@ def comp(s, regex=False):
617618
# replace ALWAYS will return a list
618619
rb = [blk if inplace else blk.copy()]
619620
for i, (s, d) in enumerate(zip(src_list, dest_list)):
621+
# TODO: assert/validate that `d` is always a scalar?
620622
new_rb = []
621623
for b in rb:
622624
m = masks[i][b.mgr_locs.indexer]

pandas/core/ops/__init__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import numpy as np
1313

14-
from pandas._libs import lib, ops as libops
14+
from pandas._libs import Timedelta, Timestamp, lib, ops as libops
1515
from pandas.errors import NullFrequencyError
1616
from pandas.util._decorators import Appender
1717

@@ -87,7 +87,7 @@ def get_op_result_name(left, right):
8787
Usually a string
8888
"""
8989
# `left` is always a pd.Series when called from within ops
90-
if isinstance(right, (ABCSeries, pd.Index)):
90+
if isinstance(right, (ABCSeries, ABCIndexClass)):
9191
name = _maybe_match_name(left, right)
9292
else:
9393
name = left.name
@@ -151,14 +151,14 @@ def maybe_upcast_for_op(obj):
151151
# GH#22390 cast up to Timedelta to rely on Timedelta
152152
# implementation; otherwise operation against numeric-dtype
153153
# raises TypeError
154-
return pd.Timedelta(obj)
154+
return Timedelta(obj)
155155
elif isinstance(obj, np.timedelta64) and not isna(obj):
156156
# In particular non-nanosecond timedelta64 needs to be cast to
157157
# nanoseconds, or else we get undesired behavior like
158158
# np.timedelta64(3, 'D') / 2 == np.timedelta64(1, 'D')
159159
# The isna check is to avoid casting timedelta64("NaT"), which would
160160
# return NaT and incorrectly be treated as a datetime-NaT.
161-
return pd.Timedelta(obj)
161+
return Timedelta(obj)
162162
elif isinstance(obj, np.ndarray) and is_timedelta64_dtype(obj):
163163
# GH#22390 Unfortunately we need to special-case right-hand
164164
# timedelta64 dtypes because numpy casts integer dtypes to
@@ -1864,7 +1864,7 @@ def wrapper(self, other, axis=None):
18641864
)
18651865
msg = "\n".join(textwrap.wrap(msg.format(future=future)))
18661866
warnings.warn(msg, FutureWarning, stacklevel=2)
1867-
other = pd.Timestamp(other)
1867+
other = Timestamp(other)
18681868

18691869
res_values = dispatch_to_index_op(op, self, other, pd.DatetimeIndex)
18701870

@@ -1890,7 +1890,7 @@ def wrapper(self, other, axis=None):
18901890
res_values, index=self.index, name=res_name
18911891
).rename(res_name)
18921892

1893-
elif isinstance(other, (np.ndarray, pd.Index)):
1893+
elif isinstance(other, (np.ndarray, ABCIndexClass)):
18941894
# do not check length of zerodim array
18951895
# as it will broadcast
18961896
if other.ndim != 0 and len(self) != len(other):

pandas/tests/computation/test_eval.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ def check_single_invert_op(self, lhs, cmp1, rhs):
411411
)
412412

413413
def check_compound_invert_op(self, lhs, cmp1, rhs):
414-
skip_these = "in", "not in"
414+
skip_these = ["in", "not in"]
415415
ex = "~(lhs {0} rhs)".format(cmp1)
416416

417417
msg = (

pandas/tests/io/json/test_pandas.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,6 +1432,7 @@ def test_to_jsonl(self):
14321432
assert result == expected
14331433
assert_frame_equal(pd.read_json(result, lines=True), df)
14341434

1435+
# TODO: there is a near-identical test for pytables; can we share?
14351436
def test_latin_encoding(self):
14361437
# GH 13774
14371438
pytest.skip("encoding not implemented in .to_json(), xref #13774")

pandas/tests/series/test_arithmetic.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class TestSeriesFlexArithmetic:
2424
],
2525
)
2626
@pytest.mark.parametrize(
27-
"opname", ["add", "sub", "mul", "floordiv", "truediv", "div", "pow"]
27+
"opname", ["add", "sub", "mul", "floordiv", "truediv", "pow"]
2828
)
2929
def test_flex_method_equivalence(self, opname, ts):
3030
# check that Series.{opname} behaves like Series.__{opname}__,
@@ -34,15 +34,8 @@ def test_flex_method_equivalence(self, opname, ts):
3434
other = ts[1](tser)
3535
check_reverse = ts[2]
3636

37-
if opname == "div":
38-
pytest.skip("div test only for Py3")
39-
4037
op = getattr(Series, opname)
41-
42-
if op == "div":
43-
alt = operator.truediv
44-
else:
45-
alt = getattr(operator, opname)
38+
alt = getattr(operator, opname)
4639

4740
result = op(series, other)
4841
expected = alt(series, other)

0 commit comments

Comments
 (0)