Skip to content

Commit d320ef7

Browse files
jbrockmendeljreback
authored andcommitted
CLN: Assorted Cleanups (pandas-dev#27791)
1 parent f00905e commit d320ef7

File tree

9 files changed

+40
-30
lines changed

9 files changed

+40
-30
lines changed

pandas/core/arrays/categorical.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
ensure_int64,
2323
ensure_object,
2424
ensure_platform_int,
25-
is_categorical,
2625
is_categorical_dtype,
2726
is_datetime64_dtype,
2827
is_datetimelike,
@@ -2659,18 +2658,18 @@ def _get_codes_for_values(values, categories):
26592658
return coerce_indexer_dtype(t.lookup(vals), cats)
26602659

26612660

2662-
def _recode_for_categories(codes, old_categories, new_categories):
2661+
def _recode_for_categories(codes: np.ndarray, old_categories, new_categories):
26632662
"""
26642663
Convert a set of codes for to a new set of categories
26652664
26662665
Parameters
26672666
----------
2668-
codes : array
2667+
codes : np.ndarray
26692668
old_categories, new_categories : Index
26702669
26712670
Returns
26722671
-------
2673-
new_codes : array
2672+
new_codes : np.ndarray[np.int64]
26742673
26752674
Examples
26762675
--------
@@ -2725,17 +2724,15 @@ def _factorize_from_iterable(values):
27252724
If `values` has a categorical dtype, then `categories` is
27262725
a CategoricalIndex keeping the categories and order of `values`.
27272726
"""
2728-
from pandas.core.indexes.category import CategoricalIndex
2729-
27302727
if not is_list_like(values):
27312728
raise TypeError("Input must be list-like")
27322729

2733-
if is_categorical(values):
2734-
values = CategoricalIndex(values)
2735-
# The CategoricalIndex level we want to build has the same categories
2730+
if is_categorical_dtype(values):
2731+
values = extract_array(values)
2732+
# The Categorical we want to build has the same categories
27362733
# as values but its codes are by def [0, ..., len(n_categories) - 1]
27372734
cat_codes = np.arange(len(values.categories), dtype=values.codes.dtype)
2738-
categories = values._create_from_codes(cat_codes)
2735+
categories = Categorical.from_codes(cat_codes, dtype=values.dtype)
27392736
codes = values.codes
27402737
else:
27412738
# The value of ordered is irrelevant since we don't use cat as such,

pandas/core/arrays/datetimelike.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ def strftime(self, date_format):
161161
162162
Returns
163163
-------
164-
Index
165-
Index of formatted strings.
164+
ndarray
165+
NumPy ndarray of formatted strings.
166166
167167
See Also
168168
--------
@@ -180,9 +180,7 @@ def strftime(self, date_format):
180180
'March 10, 2018, 09:00:02 AM'],
181181
dtype='object')
182182
"""
183-
from pandas import Index
184-
185-
return Index(self._format_native_types(date_format=date_format))
183+
return self._format_native_types(date_format=date_format).astype(object)
186184

187185

188186
class TimelikeOps:
@@ -1018,9 +1016,9 @@ def _add_delta_tdi(self, other):
10181016

10191017
if isinstance(other, np.ndarray):
10201018
# ndarray[timedelta64]; wrap in TimedeltaIndex for op
1021-
from pandas import TimedeltaIndex
1019+
from pandas.core.arrays import TimedeltaArray
10221020

1023-
other = TimedeltaIndex(other)
1021+
other = TimedeltaArray._from_sequence(other)
10241022

10251023
self_i8 = self.asi8
10261024
other_i8 = other.asi8

pandas/core/arrays/sparse.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1781,11 +1781,11 @@ def sparse_arithmetic_method(self, other):
17811781

17821782
@classmethod
17831783
def _create_comparison_method(cls, op):
1784-
def cmp_method(self, other):
1785-
op_name = op.__name__
1784+
op_name = op.__name__
1785+
if op_name in {"and_", "or_"}:
1786+
op_name = op_name[:-1]
17861787

1787-
if op_name in {"and_", "or_"}:
1788-
op_name = op_name[:-1]
1788+
def cmp_method(self, other):
17891789

17901790
if isinstance(other, (ABCSeries, ABCIndexClass)):
17911791
# Rely on pandas to unbox and dispatch to us.

pandas/core/indexes/datetimes.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class DatetimeDelegateMixin(DatetimelikeDelegateMixin):
6969
# Some are "raw" methods, the result is not not re-boxed in an Index
7070
# We also have a few "extra" attrs, which may or may not be raw,
7171
# which we we dont' want to expose in the .dt accessor.
72-
_extra_methods = ["to_period", "to_perioddelta", "to_julian_date"]
72+
_extra_methods = ["to_period", "to_perioddelta", "to_julian_date", "strftime"]
7373
_extra_raw_methods = ["to_pydatetime", "_local_timestamps", "_has_same_tz"]
7474
_extra_raw_properties = ["_box_func", "tz", "tzinfo"]
7575
_delegated_properties = DatetimeArray._datetimelike_ops + _extra_raw_properties
@@ -1184,7 +1184,6 @@ def slice_indexer(self, start=None, end=None, step=None, kind=None):
11841184
is_normalized = cache_readonly(DatetimeArray.is_normalized.fget) # type: ignore
11851185
_resolution = cache_readonly(DatetimeArray._resolution.fget) # type: ignore
11861186

1187-
strftime = ea_passthrough(DatetimeArray.strftime)
11881187
_has_same_tz = ea_passthrough(DatetimeArray._has_same_tz)
11891188

11901189
@property

pandas/core/indexes/period.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ class PeriodDelegateMixin(DatetimelikeDelegateMixin):
6363

6464
_delegate_class = PeriodArray
6565
_delegated_properties = PeriodArray._datetimelike_ops
66-
_delegated_methods = set(PeriodArray._datetimelike_methods) | {"_addsub_int_array"}
66+
_delegated_methods = set(PeriodArray._datetimelike_methods) | {
67+
"_addsub_int_array",
68+
"strftime",
69+
}
6770
_raw_properties = {"is_leap_year"}
6871

6972

pandas/io/pytables.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -3202,7 +3202,9 @@ def read(self, start=None, stop=None, **kwargs):
32023202
values = self.read_array(
32033203
"block{idx}_values".format(idx=i), start=_start, stop=_stop
32043204
)
3205-
blk = make_block(values, placement=items.get_indexer(blk_items))
3205+
blk = make_block(
3206+
values, placement=items.get_indexer(blk_items), ndim=len(axes)
3207+
)
32063208
blocks.append(blk)
32073209

32083210
return self.obj_type(BlockManager(blocks, axes))
@@ -4462,7 +4464,7 @@ def read(self, where=None, columns=None, **kwargs):
44624464
if values.ndim == 1 and isinstance(values, np.ndarray):
44634465
values = values.reshape((1, values.shape[0]))
44644466

4465-
block = make_block(values, placement=np.arange(len(cols_)))
4467+
block = make_block(values, placement=np.arange(len(cols_)), ndim=2)
44664468
mgr = BlockManager([block], [cols_, index_])
44674469
frames.append(DataFrame(mgr))
44684470

pandas/tests/arrays/test_datetimelike.py

+14
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,13 @@ def test_concat_same_type_different_freq(self):
462462

463463
tm.assert_datetime_array_equal(result, expected)
464464

465+
def test_strftime(self, datetime_index):
466+
arr = DatetimeArray(datetime_index)
467+
468+
result = arr.strftime("%Y %b")
469+
expected = np.array(datetime_index.strftime("%Y %b"))
470+
tm.assert_numpy_array_equal(result, expected)
471+
465472

466473
class TestTimedeltaArray(SharedTests):
467474
index_cls = pd.TimedeltaIndex
@@ -652,6 +659,13 @@ def test_array_interface(self, period_index):
652659
expected = np.asarray(arr).astype("S20")
653660
tm.assert_numpy_array_equal(result, expected)
654661

662+
def test_strftime(self, period_index):
663+
arr = PeriodArray(period_index)
664+
665+
result = arr.strftime("%Y")
666+
expected = np.array(period_index.strftime("%Y"))
667+
tm.assert_numpy_array_equal(result, expected)
668+
655669

656670
@pytest.mark.parametrize(
657671
"array,casting_nats",

pandas/tests/arrays/test_integer.py

-2
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,6 @@ def test_compare_array(self, data, all_compare_operators):
379379

380380

381381
class TestCasting:
382-
pass
383-
384382
@pytest.mark.parametrize("dropna", [True, False])
385383
def test_construct_index(self, all_data, dropna):
386384
# ensure that we do not coerce to Float64Index, rather

pandas/tests/series/test_period.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,9 @@ def test_NaT_scalar(self):
7171
series[2] = val
7272
assert pd.isna(series[2])
7373

74-
@pytest.mark.xfail(reason="PeriodDtype Series not supported yet")
7574
def test_NaT_cast(self):
7675
result = Series([np.nan]).astype("period[D]")
77-
expected = Series([pd.NaT])
76+
expected = Series([pd.NaT], dtype="period[D]")
7877
tm.assert_series_equal(result, expected)
7978

8079
def test_set_none(self):

0 commit comments

Comments
 (0)