Skip to content

Commit da7914b

Browse files
jbrockmendelrhshadrach
authored andcommitted
CLN: assorted cleanups (pandas-dev#33614)
1 parent fe9add0 commit da7914b

File tree

11 files changed

+25
-30
lines changed

11 files changed

+25
-30
lines changed

pandas/_libs/parsers.pyx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ from pandas.core.dtypes.common import (
5555
is_bool_dtype, is_object_dtype,
5656
is_datetime64_dtype,
5757
pandas_dtype, is_extension_array_dtype)
58-
from pandas.core.arrays import Categorical
5958
from pandas.core.dtypes.concat import union_categoricals
60-
import pandas.io.common as icom
6159

6260
from pandas.compat import _import_lzma, _get_lzma_file
6361
from pandas.errors import (ParserError, DtypeWarning,
@@ -1149,7 +1147,8 @@ cdef class TextReader:
11491147

11501148
# Method accepts list of strings, not encoded ones.
11511149
true_values = [x.decode() for x in self.true_values]
1152-
cat = Categorical._from_inferred_categories(
1150+
array_type = dtype.construct_array_type()
1151+
cat = array_type._from_inferred_categories(
11531152
cats, codes, dtype, true_values=true_values)
11541153
return cat, na_count
11551154

pandas/core/algorithms.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
is_bool_dtype,
3030
is_categorical_dtype,
3131
is_complex_dtype,
32-
is_datetime64_any_dtype,
3332
is_datetime64_dtype,
3433
is_datetime64_ns_dtype,
3534
is_extension_array_dtype,
@@ -122,12 +121,7 @@ def _ensure_data(values, dtype=None):
122121
return ensure_object(values), "object"
123122

124123
# datetimelike
125-
if (
126-
needs_i8_conversion(values)
127-
or is_period_dtype(dtype)
128-
or is_datetime64_any_dtype(dtype)
129-
or is_timedelta64_dtype(dtype)
130-
):
124+
if needs_i8_conversion(values) or needs_i8_conversion(dtype):
131125
if is_period_dtype(values) or is_period_dtype(dtype):
132126
from pandas import PeriodIndex
133127

pandas/core/arrays/datetimelike.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -638,8 +638,6 @@ def astype(self, dtype, copy=True):
638638
# 1. PeriodArray.astype handles period -> period
639639
# 2. DatetimeArray.astype handles conversion between tz.
640640
# 3. DatetimeArray.astype handles datetime -> period
641-
from pandas import Categorical
642-
643641
dtype = pandas_dtype(dtype)
644642

645643
if is_object_dtype(dtype):
@@ -667,7 +665,8 @@ def astype(self, dtype, copy=True):
667665
msg = f"Cannot cast {type(self).__name__} to dtype {dtype}"
668666
raise TypeError(msg)
669667
elif is_categorical_dtype(dtype):
670-
return Categorical(self, dtype=dtype)
668+
arr_cls = dtype.construct_array_type()
669+
return arr_cls(self, dtype=dtype)
671670
else:
672671
return np.asarray(self, dtype=dtype)
673672

pandas/core/arrays/datetimes.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
from pandas.errors import PerformanceWarning
2323

2424
from pandas.core.dtypes.common import (
25-
_INT64_DTYPE,
2625
DT64NS_DTYPE,
26+
INT64_DTYPE,
2727
is_bool_dtype,
2828
is_categorical_dtype,
2929
is_datetime64_any_dtype,
@@ -404,7 +404,7 @@ def _generate_range(
404404
start = start.tz_localize(None)
405405
if end is not None:
406406
end = end.tz_localize(None)
407-
# TODO: consider re-implementing _cached_range; GH#17914
407+
408408
values, _tz = generate_regular_range(start, end, periods, freq)
409409
index = cls._simple_new(values, freq=freq, dtype=tz_to_dtype(_tz))
410410

@@ -1963,7 +1963,7 @@ def sequence_to_dt64ns(
19631963
if tz:
19641964
tz = timezones.maybe_get_tz(tz)
19651965

1966-
if data.dtype != _INT64_DTYPE:
1966+
if data.dtype != INT64_DTYPE:
19671967
data = data.astype(np.int64, copy=False)
19681968
result = data.view(DT64NS_DTYPE)
19691969

pandas/core/base.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,8 +1143,7 @@ def _map_values(self, mapper, na_action=None):
11431143
raise NotImplementedError
11441144
map_f = lambda values, f: values.map(f)
11451145
else:
1146-
values = self.astype(object)
1147-
values = getattr(values, "values", values)
1146+
values = self.astype(object)._values
11481147
if na_action == "ignore":
11491148

11501149
def map_f(values, f):

pandas/core/dtypes/cast.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
from pandas.util._validators import validate_bool_kwarg
2222

2323
from pandas.core.dtypes.common import (
24-
_INT64_DTYPE,
2524
_POSSIBLY_CAST_DTYPES,
2625
DT64NS_DTYPE,
26+
INT64_DTYPE,
2727
TD64NS_DTYPE,
2828
ensure_int8,
2929
ensure_int16,
@@ -954,7 +954,7 @@ def astype_nansafe(arr, dtype, copy: bool = True, skipna: bool = False):
954954
raise ValueError("Cannot convert NaT values to integer")
955955
return arr.view(dtype)
956956

957-
if dtype not in [_INT64_DTYPE, TD64NS_DTYPE]:
957+
if dtype not in [INT64_DTYPE, TD64NS_DTYPE]:
958958

959959
# allow frequency conversions
960960
# we return a float here!

pandas/core/dtypes/common.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,14 @@
6060

6161
DT64NS_DTYPE = conversion.DT64NS_DTYPE
6262
TD64NS_DTYPE = conversion.TD64NS_DTYPE
63-
_INT64_DTYPE = np.dtype(np.int64)
63+
INT64_DTYPE = np.dtype(np.int64)
6464

6565
# oh the troubles to reduce import time
6666
_is_scipy_sparse = None
6767

6868
ensure_float64 = algos.ensure_float64
6969
ensure_float32 = algos.ensure_float32
7070

71-
_ensure_datetime64ns = conversion.ensure_datetime64ns
72-
_ensure_timedelta64ns = conversion.ensure_timedelta64ns
73-
7471

7572
def ensure_float(arr):
7673
"""

pandas/core/generic.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3549,8 +3549,6 @@ class animal locomotion
35493549
result._set_is_copy(self, copy=not result._is_view)
35503550
return result
35513551

3552-
_xs: Callable = xs
3553-
35543552
def __getitem__(self, item):
35553553
raise AbstractMethodError(self)
35563554

pandas/core/indexing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,7 @@ def _getitem_tuple(self, tup: Tuple):
10461046

10471047
def _get_label(self, label, axis: int):
10481048
# GH#5667 this will fail if the label is not present in the axis.
1049-
return self.obj._xs(label, axis=axis)
1049+
return self.obj.xs(label, axis=axis)
10501050

10511051
def _handle_lowerdim_multi_index_axis0(self, tup: Tuple):
10521052
# we have an axis0 multi-index, handle or raise

pandas/core/internals/blocks.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -585,8 +585,7 @@ def astype(self, dtype, copy: bool = False, errors: str = "raise"):
585585
newb = self.copy() if copy else self
586586
return newb
587587

588-
# TODO(extension)
589-
# should we make this attribute?
588+
# TODO(EA2D): special case not needed with 2D EAs
590589
if isinstance(values, np.ndarray):
591590
values = values.reshape(self.shape)
592591

@@ -1554,13 +1553,15 @@ def __init__(self, values, placement, ndim=None):
15541553

15551554
@property
15561555
def shape(self):
1556+
# TODO(EA2D): override unnecessary with 2D EAs
15571557
if self.ndim == 1:
15581558
return ((len(self.values)),)
15591559
return (len(self.mgr_locs), len(self.values))
15601560

15611561
def iget(self, col):
15621562

15631563
if self.ndim == 2 and isinstance(col, tuple):
1564+
# TODO(EA2D): unnecessary with 2D EAs
15641565
col, loc = col
15651566
if not com.is_null_slice(col) and col != 0:
15661567
raise IndexError(f"{self} only contains one item")
@@ -1669,6 +1670,7 @@ def setitem(self, indexer, value):
16691670
be a compatible shape.
16701671
"""
16711672
if isinstance(indexer, tuple):
1673+
# TODO(EA2D): not needed with 2D EAs
16721674
# we are always 1-D
16731675
indexer = indexer[0]
16741676

@@ -1678,6 +1680,7 @@ def setitem(self, indexer, value):
16781680

16791681
def get_values(self, dtype=None):
16801682
# ExtensionArrays must be iterable, so this works.
1683+
# TODO(EA2D): reshape not needed with 2D EAs
16811684
return np.asarray(self.values).reshape(self.shape)
16821685

16831686
def array_values(self) -> ExtensionArray:
@@ -1691,6 +1694,7 @@ def to_native_types(self, na_rep="nan", quoting=None, **kwargs):
16911694
values = np.asarray(values.astype(object))
16921695
values[mask] = na_rep
16931696

1697+
# TODO(EA2D): reshape not needed with 2D EAs
16941698
# we are expected to return a 2-d ndarray
16951699
return values.reshape(1, len(values))
16961700

@@ -1703,6 +1707,7 @@ def take_nd(
17031707
if fill_value is lib.no_default:
17041708
fill_value = None
17051709

1710+
# TODO(EA2D): special case not needed with 2D EAs
17061711
# axis doesn't matter; we are really a single-dim object
17071712
# but are passed the axis depending on the calling routing
17081713
# if its REALLY axis 0, then this will be a reindex and not a take
@@ -2229,6 +2234,7 @@ def diff(self, n: int, axis: int = 0) -> List["Block"]:
22292234
by apply.
22302235
"""
22312236
if axis == 0:
2237+
# TODO(EA2D): special case not needed with 2D EAs
22322238
# Cannot currently calculate diff across multiple blocks since this
22332239
# function is invoked via apply
22342240
raise NotImplementedError
@@ -2280,7 +2286,7 @@ def quantile(self, qs, interpolation="linear", axis=0):
22802286
blk = self.make_block(naive)
22812287
res_blk = blk.quantile(qs, interpolation=interpolation, axis=axis)
22822288

2283-
# ravel is kludge for 2D block with 1D values, assumes column-like
2289+
# TODO(EA2D): ravel is kludge for 2D block with 1D values, assumes column-like
22842290
aware = self._holder(res_blk.values.ravel(), dtype=self.dtype)
22852291
return self.make_block_same_class(aware, ndim=res_blk.ndim)
22862292

@@ -2693,6 +2699,7 @@ def make_block(values, placement, klass=None, ndim=None, dtype=None):
26932699
if isinstance(values, ABCPandasArray):
26942700
values = values.to_numpy()
26952701
if ndim and ndim > 1:
2702+
# TODO(EA2D): special case not needed with 2D EAs
26962703
values = np.atleast_2d(values)
26972704

26982705
if isinstance(dtype, PandasDtype):
@@ -2759,6 +2766,7 @@ def _safe_reshape(arr, new_shape):
27592766
if isinstance(arr, ABCSeries):
27602767
arr = arr._values
27612768
if not isinstance(arr, ABCExtensionArray):
2769+
# TODO(EA2D): special case not needed with 2D EAs
27622770
arr = arr.reshape(new_shape)
27632771
return arr
27642772

pandas/core/internals/concat.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ def get_reindexed_values(self, empty_dtype, upcasted_na):
251251
):
252252
if self.block is None:
253253
array = empty_dtype.construct_array_type()
254+
# TODO(EA2D): special case unneeded with 2D EAs
254255
return array(
255256
np.full(self.shape[1], fill_value.value), dtype=empty_dtype
256257
)

0 commit comments

Comments
 (0)