diff --git a/pandas/_libs/internals.pyx b/pandas/_libs/internals.pyx index 9fa84a0135a5e..78853ce6e41dc 100644 --- a/pandas/_libs/internals.pyx +++ b/pandas/_libs/internals.pyx @@ -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] @@ -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() diff --git a/pandas/_libs/join.pyx b/pandas/_libs/join.pyx index c9a4b49f90037..dc3bb09c1b462 100644 --- a/pandas/_libs/join.pyx +++ b/pandas/_libs/join.pyx @@ -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) diff --git a/pandas/_libs/tslibs/offsets.pyx b/pandas/_libs/tslibs/offsets.pyx index 5557882e7e9b9..e8248eeb07395 100644 --- a/pandas/_libs/tslibs/offsets.pyx +++ b/pandas/_libs/tslibs/offsets.pyx @@ -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: diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index b5c68fb7ada54..791c1110e3cd2 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -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 diff --git a/pandas/core/internals/construction.py b/pandas/core/internals/construction.py index 268375864b1df..c1fd29615e1bc 100644 --- a/pandas/core/internals/construction.py +++ b/pandas/core/internals/construction.py @@ -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}'") diff --git a/pandas/core/sorting.py b/pandas/core/sorting.py index a8348b0c5773f..7813182222d67 100644 --- a/pandas/core/sorting.py +++ b/pandas/core/sorting.py @@ -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 @@ -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 diff --git a/pandas/tests/base/test_fillna.py b/pandas/tests/base/test_fillna.py index c6f58af4c5c3a..32c9d288e665d 100644 --- a/pandas/tests/base/test_fillna.py +++ b/pandas/tests/base/test_fillna.py @@ -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 @@ -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 diff --git a/pandas/tests/base/test_unique.py b/pandas/tests/base/test_unique.py index 1f6b0f1db55d6..95e07583bab66 100644 --- a/pandas/tests/base/test_unique.py +++ b/pandas/tests/base/test_unique.py @@ -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)) diff --git a/pandas/tests/base/test_value_counts.py b/pandas/tests/base/test_value_counts.py index 10f391a49d98f..5431baf493260 100644 --- a/pandas/tests/base/test_value_counts.py +++ b/pandas/tests/base/test_value_counts.py @@ -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, @@ -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)) diff --git a/pandas/tests/indexes/common.py b/pandas/tests/indexes/common.py index 1c3739d9aebb8..7566c17eda9e6 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -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 ( @@ -37,7 +36,6 @@ Int64Index, UInt64Index, ) -from pandas.core.indexes.datetimelike import DatetimeIndexOpsMixin class Base: @@ -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 diff --git a/pandas/tests/indexes/test_common.py b/pandas/tests/indexes/test_common.py index 604b68cfcc791..ed9243a5ba8d0 100644 --- a/pandas/tests/indexes/test_common.py +++ b/pandas/tests/indexes/test_common.py @@ -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, ) @@ -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 @@ -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"])