Skip to content

Commit c4cce9b

Browse files
authored
CLN: collected cleanups (pandas-dev#44063)
1 parent 4f36b02 commit c4cce9b

File tree

11 files changed

+34
-79
lines changed

11 files changed

+34
-79
lines changed

pandas/_libs/internals.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ cdef slice indexer_as_slice(intp_t[:] vals):
408408
int64_t d
409409

410410
if vals is None:
411-
raise TypeError("vals must be ndarray")
411+
raise TypeError("vals must be ndarray") # pragma: no cover
412412

413413
n = vals.shape[0]
414414

@@ -772,7 +772,7 @@ cdef class BlockManager:
772772
self.blocks = blocks
773773
self.axes = axes
774774

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

778778
self._post_setstate()

pandas/_libs/join.pyx

+1-2
Original file line numberDiff line numberDiff line change
@@ -952,12 +952,11 @@ def asof_join_nearest(numeric_t[:] left_values,
952952
tolerance=None):
953953

954954
cdef:
955-
Py_ssize_t left_size, right_size, i
955+
Py_ssize_t left_size, i
956956
ndarray[intp_t] left_indexer, right_indexer, bli, bri, fli, fri
957957
numeric_t bdiff, fdiff
958958

959959
left_size = len(left_values)
960-
right_size = len(right_values)
961960

962961
left_indexer = np.empty(left_size, dtype=np.intp)
963962
right_indexer = np.empty(left_size, dtype=np.intp)

pandas/_libs/tslibs/offsets.pyx

+1-3
Original file line numberDiff line numberDiff line change
@@ -808,9 +808,7 @@ cdef class Tick(SingleConstructorOffset):
808808
def nanos(self) -> int64_t:
809809
return self.n * self._nanos_inc
810810

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

816814
def is_anchored(self) -> bool:

pandas/core/indexes/datetimelike.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,11 @@ def _get_insert_freq(self, loc: int, item):
670670
freq = self.freq
671671
else:
672672
# Adding a single item to an empty index may preserve freq
673-
if self.freq.is_on_offset(item):
673+
if isinstance(self.freq, Tick):
674+
# all TimedeltaIndex cases go through here; is_on_offset
675+
# would raise TypeError
676+
freq = self.freq
677+
elif self.freq.is_on_offset(item):
674678
freq = self.freq
675679
return freq
676680

pandas/core/internals/construction.py

-3
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,6 @@ def arrays_to_mgr(
155155
arrays, axes, consolidate=consolidate
156156
)
157157
elif typ == "array":
158-
if len(columns) != len(arrays):
159-
assert len(arrays) == 0
160-
arrays = [np.array([], dtype=object) for _ in range(len(columns))]
161158
return ArrayManager(arrays, [index, columns])
162159
else:
163160
raise ValueError(f"'typ' needs to be one of {{'block', 'array'}}, got '{typ}'")

pandas/core/sorting.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ def get_indexer_indexer(
9696
return indexer
9797

9898

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

652654

653655
def compress_group_index(
654-
group_index: np.ndarray, sort: bool = True
656+
group_index: npt.NDArray[np.int64], sort: bool = True
655657
) -> tuple[npt.NDArray[np.int64], npt.NDArray[np.int64]]:
656658
"""
657659
Group_index is offsets into cartesian product of all possible labels. This

pandas/tests/base/test_fillna.py

+4-14
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
import numpy as np
77
import pytest
88

9-
from pandas._libs import iNaT
10-
11-
from pandas.core.dtypes.common import needs_i8_conversion
129
from pandas.core.dtypes.generic import ABCMultiIndex
1310

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

50-
values = obj.values
47+
values = obj._values
5148
fill_value = values[0]
5249
expected = values.copy()
53-
if needs_i8_conversion(obj.dtype):
54-
values[0:2] = iNaT
55-
expected[0:2] = fill_value
56-
else:
57-
values[0:2] = null_obj
58-
expected[0:2] = fill_value
50+
values[0:2] = null_obj
51+
expected[0:2] = fill_value
5952

6053
expected = klass(expected)
6154
obj = klass(values)
6255

6356
result = obj.fillna(fill_value)
64-
if isinstance(obj, Index):
65-
tm.assert_index_equal(result, expected)
66-
else:
67-
tm.assert_series_equal(result, expected)
57+
tm.assert_equal(result, expected)
6858

6959
# check shallow_copied
7060
assert obj is not result

pandas/tests/base/test_unique.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,8 @@ def test_nunique_null(null_obj, index_or_series_obj):
9696
elif isinstance(obj, pd.MultiIndex):
9797
pytest.skip(f"MultiIndex can't hold '{null_obj}'")
9898

99-
values = obj.values
100-
if needs_i8_conversion(obj.dtype):
101-
values[0:2] = iNaT
102-
else:
103-
values[0:2] = null_obj
99+
values = obj._values
100+
values[0:2] = null_obj
104101

105102
klass = type(obj)
106103
repeated_values = np.repeat(values, range(1, len(values) + 1))

pandas/tests/base/test_value_counts.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@
55
import numpy as np
66
import pytest
77

8-
from pandas._libs import iNaT
98
from pandas.compat import np_array_datetime64_compat
109

11-
from pandas.core.dtypes.common import needs_i8_conversion
12-
1310
import pandas as pd
1411
from pandas import (
1512
DatetimeIndex,
@@ -54,11 +51,8 @@ def test_value_counts_null(null_obj, index_or_series_obj):
5451
elif isinstance(orig, pd.MultiIndex):
5552
pytest.skip(f"MultiIndex can't hold '{null_obj}'")
5653

57-
values = obj.values
58-
if needs_i8_conversion(obj.dtype):
59-
values[0:2] = iNaT
60-
else:
61-
values[0:2] = null_obj
54+
values = obj._values
55+
values[0:2] = null_obj
6256

6357
klass = type(obj)
6458
repeated_values = np.repeat(values, range(1, len(values) + 1))

pandas/tests/indexes/common.py

+3-11
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import numpy as np
77
import pytest
88

9-
from pandas._libs import iNaT
109
from pandas._libs.tslibs import Timestamp
1110

1211
from pandas.core.dtypes.common import (
@@ -37,7 +36,6 @@
3736
Int64Index,
3837
UInt64Index,
3938
)
40-
from pandas.core.indexes.datetimelike import DatetimeIndexOpsMixin
4139

4240

4341
class Base:
@@ -548,17 +546,11 @@ def test_fillna(self, index):
548546
idx.fillna([idx[0]])
549547

550548
idx = index.copy(deep=True)
551-
values = np.asarray(idx.values)
549+
values = idx._values
552550

553-
if isinstance(index, DatetimeIndexOpsMixin):
554-
values[1] = iNaT
555-
else:
556-
values[1] = np.nan
551+
values[1] = np.nan
557552

558-
if isinstance(index, PeriodIndex):
559-
idx = type(index)(values, freq=index.freq)
560-
else:
561-
idx = type(index)(values)
553+
idx = type(index)(values)
562554

563555
expected = np.array([False] * len(idx), dtype=bool)
564556
expected[1] = True

pandas/tests/indexes/test_common.py

+10-28
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@
88
import numpy as np
99
import pytest
1010

11-
from pandas._libs.tslibs import iNaT
1211
from pandas.compat import IS64
1312

1413
from pandas.core.dtypes.common import (
1514
is_integer_dtype,
16-
is_period_dtype,
1715
needs_i8_conversion,
1816
)
1917

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

176-
if is_period_dtype(index.dtype):
177-
vals = index[[0] * 5]._data
178-
vals[0] = pd.NaT
179-
elif needs_i8_conversion(index.dtype):
180-
vals = index._data._ndarray[[0] * 5]
181-
vals[0] = iNaT
182-
else:
183-
vals = index.values[[0] * 5]
184-
vals[0] = np.nan
174+
vals = index._values[[0] * 5]
175+
vals[0] = np.nan
185176

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

380367
idx = index.copy(deep=True)
381-
values = np.asarray(idx.values)
368+
values = idx._values
382369

383370
if len(index) == 0:
384371
return
385372
elif isinstance(index, NumericIndex) and is_integer_dtype(index.dtype):
386373
return
387-
elif needs_i8_conversion(index.dtype):
388-
values[1] = iNaT
389-
else:
390-
values[1] = np.nan
391374

392-
if isinstance(index, PeriodIndex):
393-
idx = type(index)(values, freq=index.freq)
394-
else:
395-
idx = type(index)(values)
375+
values[1] = np.nan
396376

397-
expected = np.array([False] * len(idx), dtype=bool)
398-
expected[1] = True
399-
tm.assert_numpy_array_equal(idx._isnan, expected)
400-
assert idx.hasnans is True
377+
idx = type(index)(values)
378+
379+
expected = np.array([False] * len(idx), dtype=bool)
380+
expected[1] = True
381+
tm.assert_numpy_array_equal(idx._isnan, expected)
382+
assert idx.hasnans is True
401383

402384

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

0 commit comments

Comments
 (0)