From f0cdf1d5fc1aa9afd60a2c47bd808cd387ff6ebe Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Wed, 12 Feb 2020 13:08:00 -0800 Subject: [PATCH 1/2] CLN: assorted cleanups --- asv_bench/benchmarks/binary_ops.py | 14 ++++++++++++++ pandas/core/arrays/datetimes.py | 3 ++- pandas/core/generic.py | 1 + pandas/core/internals/managers.py | 1 - pandas/core/ops/array_ops.py | 4 ++-- pandas/core/series.py | 2 +- pandas/tests/indexing/test_chaining_and_caching.py | 11 ++++------- 7 files changed, 24 insertions(+), 12 deletions(-) diff --git a/asv_bench/benchmarks/binary_ops.py b/asv_bench/benchmarks/binary_ops.py index 64e067d25a454..3ae18590983b9 100644 --- a/asv_bench/benchmarks/binary_ops.py +++ b/asv_bench/benchmarks/binary_ops.py @@ -41,6 +41,20 @@ def time_frame_op_with_scalar(self, dtype, scalar, op): op(self.df, scalar) +class TimeOpWithFillValue: + def setup(self): + # GH#31300 + arr = np.arange(10 ** 6) + df = DataFrame({"A": arr}) + ser = df["A"] + + self.df = df + self.ser = ser + + def time_op_with_fill_value_no_nas(self): + self.df.add(self.ser, fill_value=4) + + class Ops: params = [[True, False], ["default", 1]] diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 5888600d2fa8e..a75536e46e60d 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -283,7 +283,8 @@ def __init__(self, values, dtype=_NS_DTYPE, freq=None, copy=False): @classmethod def _simple_new(cls, values, freq=None, dtype=_NS_DTYPE): assert isinstance(values, np.ndarray) - if values.dtype == "i8": + if values.dtype != _NS_DTYPE: + assert values.dtype == "i8" values = values.view(_NS_DTYPE) result = object.__new__(cls) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 934c4c6e92bbe..adfb553d40ff0 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3504,6 +3504,7 @@ def _slice(self: FrameOrSeries, slobj: slice, axis=0) -> FrameOrSeries: Slicing with this method is *always* positional. """ + assert isinstance(slobj, slice), type(slobj) axis = self._get_block_manager_axis(axis) result = self._constructor(self._data.get_slice(slobj, axis=axis)) result = result.__finalize__(self) diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index 3dc7dd7d81530..37a4b43648bb1 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -1316,7 +1316,6 @@ def _slice_take_blocks_ax0(self, slice_or_indexer, fill_tuple=None): return blocks def _make_na_block(self, placement, fill_value=None): - # TODO: infer dtypes other than float64 from fill_value if fill_value is None: fill_value = np.nan diff --git a/pandas/core/ops/array_ops.py b/pandas/core/ops/array_ops.py index 5d53856729d0c..37a4a6eddaebe 100644 --- a/pandas/core/ops/array_ops.py +++ b/pandas/core/ops/array_ops.py @@ -43,9 +43,9 @@ def comp_method_OBJECT_ARRAY(op, x, y): if isinstance(y, list): y = construct_1d_object_array_from_listlike(y) - # TODO: Should the checks below be ABCIndexClass? if isinstance(y, (np.ndarray, ABCSeries, ABCIndex)): - # TODO: should this be ABCIndexClass?? + # Note: these checks can be for ABCIndex and not ABCIndexClass + # because that is the only object-dtype class. if not is_object_dtype(y.dtype): y = y.astype(np.object_) diff --git a/pandas/core/series.py b/pandas/core/series.py index 24e794014a15f..8577a7fb904dc 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -4378,7 +4378,7 @@ def between(self, left, right, inclusive=True) -> "Series": # Convert to types that support pd.NA def _convert_dtypes( - self: ABCSeries, + self, infer_objects: bool = True, convert_string: bool = True, convert_integer: bool = True, diff --git a/pandas/tests/indexing/test_chaining_and_caching.py b/pandas/tests/indexing/test_chaining_and_caching.py index e845487ffca9a..17722e949df1e 100644 --- a/pandas/tests/indexing/test_chaining_and_caching.py +++ b/pandas/tests/indexing/test_chaining_and_caching.py @@ -346,20 +346,17 @@ def test_chained_getitem_with_lists(self): # GH6394 # Regression in chained getitem indexing with embedded list-like from # 0.12 - def check(result, expected): - tm.assert_numpy_array_equal(result, expected) - assert isinstance(result, np.ndarray) df = DataFrame({"A": 5 * [np.zeros(3)], "B": 5 * [np.ones(3)]}) expected = df["A"].iloc[2] result = df.loc[2, "A"] - check(result, expected) + tm.assert_numpy_array_equal(result, expected) result2 = df.iloc[2]["A"] - check(result2, expected) + tm.assert_numpy_array_equal(result2, expected) result3 = df["A"].loc[2] - check(result3, expected) + tm.assert_numpy_array_equal(result3, expected) result4 = df["A"].iloc[2] - check(result4, expected) + tm.assert_numpy_array_equal(result4, expected) def test_cache_updating(self): # GH 4939, make sure to update the cache on setitem From 192581e2932ad437fa8a6c270335b101c9ec8f73 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Wed, 12 Feb 2020 13:48:01 -0800 Subject: [PATCH 2/2] revert broken --- asv_bench/benchmarks/binary_ops.py | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/asv_bench/benchmarks/binary_ops.py b/asv_bench/benchmarks/binary_ops.py index 3ae18590983b9..64e067d25a454 100644 --- a/asv_bench/benchmarks/binary_ops.py +++ b/asv_bench/benchmarks/binary_ops.py @@ -41,20 +41,6 @@ def time_frame_op_with_scalar(self, dtype, scalar, op): op(self.df, scalar) -class TimeOpWithFillValue: - def setup(self): - # GH#31300 - arr = np.arange(10 ** 6) - df = DataFrame({"A": arr}) - ser = df["A"] - - self.df = df - self.ser = ser - - def time_op_with_fill_value_no_nas(self): - self.df.add(self.ser, fill_value=4) - - class Ops: params = [[True, False], ["default", 1]]