Skip to content

Commit

Permalink
CLN: collected cleanups (pandas-dev#44063)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored Oct 18, 2021
1 parent 4f36b02 commit c4cce9b
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 79 deletions.
4 changes: 2 additions & 2 deletions pandas/_libs/internals.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ cdef slice indexer_as_slice(intp_t[:] vals):
int64_t d

if vals is None:
raise TypeError("vals must be ndarray")
raise TypeError("vals must be ndarray") # pragma: no cover

n = vals.shape[0]

Expand Down Expand Up @@ -772,7 +772,7 @@ cdef class BlockManager:
self.blocks = blocks
self.axes = axes

else:
else: # pragma: no cover
raise NotImplementedError("pre-0.14.1 pickles are no longer supported")

self._post_setstate()
Expand Down
3 changes: 1 addition & 2 deletions pandas/_libs/join.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -952,12 +952,11 @@ def asof_join_nearest(numeric_t[:] left_values,
tolerance=None):

cdef:
Py_ssize_t left_size, right_size, i
Py_ssize_t left_size, i
ndarray[intp_t] left_indexer, right_indexer, bli, bri, fli, fri
numeric_t bdiff, fdiff

left_size = len(left_values)
right_size = len(right_values)

left_indexer = np.empty(left_size, dtype=np.intp)
right_indexer = np.empty(left_size, dtype=np.intp)
Expand Down
4 changes: 1 addition & 3 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -808,9 +808,7 @@ cdef class Tick(SingleConstructorOffset):
def nanos(self) -> int64_t:
return self.n * self._nanos_inc

# FIXME: This should be typed as datetime, but we DatetimeLikeIndex.insert
# checks self.freq.is_on_offset with a Timedelta sometimes.
def is_on_offset(self, dt) -> bool:
def is_on_offset(self, dt: datetime) -> bool:
return True

def is_anchored(self) -> bool:
Expand Down
6 changes: 5 additions & 1 deletion pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,11 @@ def _get_insert_freq(self, loc: int, item):
freq = self.freq
else:
# Adding a single item to an empty index may preserve freq
if self.freq.is_on_offset(item):
if isinstance(self.freq, Tick):
# all TimedeltaIndex cases go through here; is_on_offset
# would raise TypeError
freq = self.freq
elif self.freq.is_on_offset(item):
freq = self.freq
return freq

Expand Down
3 changes: 0 additions & 3 deletions pandas/core/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,6 @@ def arrays_to_mgr(
arrays, axes, consolidate=consolidate
)
elif typ == "array":
if len(columns) != len(arrays):
assert len(arrays) == 0
arrays = [np.array([], dtype=object) for _ in range(len(columns))]
return ArrayManager(arrays, [index, columns])
else:
raise ValueError(f"'typ' needs to be one of {{'block', 'array'}}, got '{typ}'")
Expand Down
6 changes: 4 additions & 2 deletions pandas/core/sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ def get_indexer_indexer(
return indexer


def get_group_index(labels, shape: Shape, sort: bool, xnull: bool):
def get_group_index(
labels, shape: Shape, sort: bool, xnull: bool
) -> npt.NDArray[np.int64]:
"""
For the particular label_list, gets the offsets into the hypothetical list
representing the totally ordered cartesian product of all possible label
Expand Down Expand Up @@ -651,7 +653,7 @@ def get_group_index_sorter(


def compress_group_index(
group_index: np.ndarray, sort: bool = True
group_index: npt.NDArray[np.int64], sort: bool = True
) -> tuple[npt.NDArray[np.int64], npt.NDArray[np.int64]]:
"""
Group_index is offsets into cartesian product of all possible labels. This
Expand Down
18 changes: 4 additions & 14 deletions pandas/tests/base/test_fillna.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
import numpy as np
import pytest

from pandas._libs import iNaT

from pandas.core.dtypes.common import needs_i8_conversion
from pandas.core.dtypes.generic import ABCMultiIndex

from pandas import Index
Expand Down Expand Up @@ -47,24 +44,17 @@ def test_fillna_null(null_obj, index_or_series_obj):
elif isinstance(obj, ABCMultiIndex):
pytest.skip(f"MultiIndex can't hold '{null_obj}'")

values = obj.values
values = obj._values
fill_value = values[0]
expected = values.copy()
if needs_i8_conversion(obj.dtype):
values[0:2] = iNaT
expected[0:2] = fill_value
else:
values[0:2] = null_obj
expected[0:2] = fill_value
values[0:2] = null_obj
expected[0:2] = fill_value

expected = klass(expected)
obj = klass(values)

result = obj.fillna(fill_value)
if isinstance(obj, Index):
tm.assert_index_equal(result, expected)
else:
tm.assert_series_equal(result, expected)
tm.assert_equal(result, expected)

# check shallow_copied
assert obj is not result
7 changes: 2 additions & 5 deletions pandas/tests/base/test_unique.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,8 @@ def test_nunique_null(null_obj, index_or_series_obj):
elif isinstance(obj, pd.MultiIndex):
pytest.skip(f"MultiIndex can't hold '{null_obj}'")

values = obj.values
if needs_i8_conversion(obj.dtype):
values[0:2] = iNaT
else:
values[0:2] = null_obj
values = obj._values
values[0:2] = null_obj

klass = type(obj)
repeated_values = np.repeat(values, range(1, len(values) + 1))
Expand Down
10 changes: 2 additions & 8 deletions pandas/tests/base/test_value_counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
import numpy as np
import pytest

from pandas._libs import iNaT
from pandas.compat import np_array_datetime64_compat

from pandas.core.dtypes.common import needs_i8_conversion

import pandas as pd
from pandas import (
DatetimeIndex,
Expand Down Expand Up @@ -54,11 +51,8 @@ def test_value_counts_null(null_obj, index_or_series_obj):
elif isinstance(orig, pd.MultiIndex):
pytest.skip(f"MultiIndex can't hold '{null_obj}'")

values = obj.values
if needs_i8_conversion(obj.dtype):
values[0:2] = iNaT
else:
values[0:2] = null_obj
values = obj._values
values[0:2] = null_obj

klass = type(obj)
repeated_values = np.repeat(values, range(1, len(values) + 1))
Expand Down
14 changes: 3 additions & 11 deletions pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import numpy as np
import pytest

from pandas._libs import iNaT
from pandas._libs.tslibs import Timestamp

from pandas.core.dtypes.common import (
Expand Down Expand Up @@ -37,7 +36,6 @@
Int64Index,
UInt64Index,
)
from pandas.core.indexes.datetimelike import DatetimeIndexOpsMixin


class Base:
Expand Down Expand Up @@ -548,17 +546,11 @@ def test_fillna(self, index):
idx.fillna([idx[0]])

idx = index.copy(deep=True)
values = np.asarray(idx.values)
values = idx._values

if isinstance(index, DatetimeIndexOpsMixin):
values[1] = iNaT
else:
values[1] = np.nan
values[1] = np.nan

if isinstance(index, PeriodIndex):
idx = type(index)(values, freq=index.freq)
else:
idx = type(index)(values)
idx = type(index)(values)

expected = np.array([False] * len(idx), dtype=bool)
expected[1] = True
Expand Down
38 changes: 10 additions & 28 deletions pandas/tests/indexes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
import numpy as np
import pytest

from pandas._libs.tslibs import iNaT
from pandas.compat import IS64

from pandas.core.dtypes.common import (
is_integer_dtype,
is_period_dtype,
needs_i8_conversion,
)

Expand Down Expand Up @@ -173,21 +171,10 @@ def test_unique(self, index_flat):
if not index._can_hold_na:
pytest.skip("Skip na-check if index cannot hold na")

if is_period_dtype(index.dtype):
vals = index[[0] * 5]._data
vals[0] = pd.NaT
elif needs_i8_conversion(index.dtype):
vals = index._data._ndarray[[0] * 5]
vals[0] = iNaT
else:
vals = index.values[[0] * 5]
vals[0] = np.nan
vals = index._values[[0] * 5]
vals[0] = np.nan

vals_unique = vals[:2]
if index.dtype.kind in ["m", "M"]:
# i.e. needs_i8_conversion but not period_dtype, as above
vals = type(index._data)(vals, dtype=index.dtype)
vals_unique = type(index._data)._simple_new(vals_unique, dtype=index.dtype)
idx_nan = index._shallow_copy(vals)
idx_unique_nan = index._shallow_copy(vals_unique)
assert idx_unique_nan.is_unique is True
Expand Down Expand Up @@ -378,26 +365,21 @@ def test_hasnans_isnans(self, index_flat):
assert idx.hasnans is False

idx = index.copy(deep=True)
values = np.asarray(idx.values)
values = idx._values

if len(index) == 0:
return
elif isinstance(index, NumericIndex) and is_integer_dtype(index.dtype):
return
elif needs_i8_conversion(index.dtype):
values[1] = iNaT
else:
values[1] = np.nan

if isinstance(index, PeriodIndex):
idx = type(index)(values, freq=index.freq)
else:
idx = type(index)(values)
values[1] = np.nan

expected = np.array([False] * len(idx), dtype=bool)
expected[1] = True
tm.assert_numpy_array_equal(idx._isnan, expected)
assert idx.hasnans is True
idx = type(index)(values)

expected = np.array([False] * len(idx), dtype=bool)
expected[1] = True
tm.assert_numpy_array_equal(idx._isnan, expected)
assert idx.hasnans is True


@pytest.mark.parametrize("na_position", [None, "middle"])
Expand Down

0 comments on commit c4cce9b

Please sign in to comment.