Skip to content

Commit 95eee9d

Browse files
authored
CLN: assorted cleanups (#39856)
1 parent 2e5c28f commit 95eee9d

File tree

10 files changed

+104
-125
lines changed

10 files changed

+104
-125
lines changed

Makefile

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,3 @@ doc:
2525
cd doc; \
2626
python make.py clean; \
2727
python make.py html
28-
29-
check:
30-
python3 scripts/validate_unwanted_patterns.py \
31-
--validation-type="private_function_across_module" \
32-
--included-file-extensions="py" \
33-
--excluded-file-paths=pandas/tests,asv_bench/ \
34-
pandas/
35-
36-
python3 scripts/validate_unwanted_patterns.py \
37-
--validation-type="private_import_across_module" \
38-
--included-file-extensions="py" \
39-
--excluded-file-paths=pandas/tests,asv_bench/,doc/
40-
pandas/

pandas/core/common.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,7 @@
4949
ABCSeries,
5050
)
5151
from pandas.core.dtypes.inference import iterable_not_string
52-
from pandas.core.dtypes.missing import ( # noqa
53-
isna,
54-
isnull,
55-
notnull,
56-
)
52+
from pandas.core.dtypes.missing import isna
5753

5854

5955
class SettingWithCopyError(ValueError):

pandas/core/frame.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -752,10 +752,11 @@ def _can_fast_transpose(self) -> bool:
752752
"""
753753
if isinstance(self._mgr, ArrayManager):
754754
return False
755-
if self._mgr.any_extension_types:
756-
# TODO(EA2D) special case would be unnecessary with 2D EAs
755+
blocks = self._mgr.blocks
756+
if len(blocks) != 1:
757757
return False
758-
return len(self._mgr.blocks) == 1
758+
759+
return not self._mgr.any_extension_types
759760

760761
# ----------------------------------------------------------------------
761762
# Rendering Methods

pandas/core/indexes/interval.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -822,9 +822,7 @@ def _convert_list_indexer(self, keyarr):
822822
def _is_comparable_dtype(self, dtype: DtypeObj) -> bool:
823823
if not isinstance(dtype, IntervalDtype):
824824
return False
825-
if self.closed != dtype.closed:
826-
return False
827-
common_subtype = find_common_type([self.dtype.subtype, dtype.subtype])
825+
common_subtype = find_common_type([self.dtype, dtype])
828826
return not is_object_dtype(common_subtype)
829827

830828
# --------------------------------------------------------------------

pandas/core/internals/blocks.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,7 @@ def setitem(self, indexer, value):
10131013
arr_value = value
10141014
else:
10151015
is_ea_value = False
1016-
arr_value = np.array(value)
1016+
arr_value = np.asarray(value)
10171017

10181018
if transpose:
10191019
values = values.T
@@ -1654,8 +1654,8 @@ def setitem(self, indexer, value):
16541654
be a compatible shape.
16551655
"""
16561656
if not self._can_hold_element(value):
1657-
# This is only relevant for DatetimeTZBlock, which has a
1658-
# non-trivial `_can_hold_element`.
1657+
# This is only relevant for DatetimeTZBlock, ObjectValuesExtensionBlock,
1658+
# which has a non-trivial `_can_hold_element`.
16591659
# https://github.com/pandas-dev/pandas/issues/24020
16601660
# Need a dedicated setitem until GH#24020 (type promotion in setitem
16611661
# for extension arrays) is designed and implemented.
@@ -2081,6 +2081,8 @@ def fillna(
20812081
class DatetimeLikeBlockMixin(NDArrayBackedExtensionBlock):
20822082
"""Mixin class for DatetimeBlock, DatetimeTZBlock, and TimedeltaBlock."""
20832083

2084+
is_numeric = False
2085+
_can_hold_na = True
20842086
_dtype: np.dtype
20852087
_holder: Type[Union[DatetimeArray, TimedeltaArray]]
20862088

@@ -2133,10 +2135,6 @@ class DatetimeBlock(DatetimeLikeBlockMixin):
21332135
_dtype = fill_value.dtype
21342136
_holder = DatetimeArray
21352137

2136-
@property
2137-
def _can_hold_na(self):
2138-
return True
2139-
21402138
def set_inplace(self, locs, values):
21412139
"""
21422140
See Block.set.__doc__
@@ -2153,6 +2151,8 @@ class DatetimeTZBlock(ExtensionBlock, DatetimeBlock):
21532151

21542152
__slots__ = ()
21552153
is_extension = True
2154+
_can_hold_na = True
2155+
is_numeric = False
21562156

21572157
_holder = DatetimeArray
21582158

@@ -2205,8 +2205,6 @@ def external_values(self):
22052205

22062206
class TimeDeltaBlock(DatetimeLikeBlockMixin):
22072207
__slots__ = ()
2208-
_can_hold_na = True
2209-
is_numeric = False
22102208
_holder = TimedeltaArray
22112209
fill_value = np.timedelta64("NaT", "ns")
22122210
_dtype = fill_value.dtype

pandas/core/internals/concat.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,8 @@ def concatenate_block_managers(
118118
else:
119119
b = make_block(values, placement=placement, ndim=blk.ndim)
120120
else:
121-
b = make_block(
122-
_concatenate_join_units(join_units, concat_axis, copy=copy),
123-
placement=placement,
124-
ndim=len(axes),
125-
)
121+
new_values = _concatenate_join_units(join_units, concat_axis, copy=copy)
122+
b = make_block(new_values, placement=placement, ndim=len(axes))
126123
blocks.append(b)
127124

128125
return BlockManager(blocks, axes)

pandas/core/reshape/reshape.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ def unstack(obj, level, fill_value=None):
429429
return obj.T.stack(dropna=False)
430430
elif not isinstance(obj.index, MultiIndex):
431431
# GH 36113
432-
# Give nicer error messages when unstack a Series whose
432+
# Give nicer error messages when unstack a Series whose
433433
# Index is not a MultiIndex.
434434
raise ValueError(
435435
f"index must be a MultiIndex to unstack, {type(obj.index)} was passed"
@@ -451,9 +451,10 @@ def _unstack_frame(obj, level, fill_value=None):
451451
mgr = obj._mgr.unstack(unstacker, fill_value=fill_value)
452452
return obj._constructor(mgr)
453453
else:
454-
return _Unstacker(
455-
obj.index, level=level, constructor=obj._constructor
456-
).get_result(obj._values, value_columns=obj.columns, fill_value=fill_value)
454+
unstacker = _Unstacker(obj.index, level=level, constructor=obj._constructor)
455+
return unstacker.get_result(
456+
obj._values, value_columns=obj.columns, fill_value=fill_value
457+
)
457458

458459

459460
def _unstack_extension_series(series, level, fill_value):

pandas/tests/arithmetic/test_timedelta64.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,6 +2090,7 @@ def test_td64arr_div_numeric_array(
20902090
expected = [tdser[n] / vector[n] for n in range(len(tdser))]
20912091
expected = pd.Index(expected) # do dtype inference
20922092
expected = tm.box_expected(expected, xbox)
2093+
assert tm.get_dtype(expected) == "m8[ns]"
20932094

20942095
if using_array_manager and box_with_array is pd.DataFrame:
20952096
# TODO the behaviour is buggy here (third column with all-NaT

pandas/tests/indexing/test_iloc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,7 @@ def test_iloc_setitem_empty_frame_raises_with_3d_ndarray(self):
921921
with pytest.raises(ValueError, match=msg):
922922
obj.iloc[nd3] = 0
923923

924-
@pytest.mark.parametrize("indexer", [lambda x: x.loc, lambda x: x.iloc])
924+
@pytest.mark.parametrize("indexer", [tm.loc, tm.iloc])
925925
def test_iloc_getitem_read_only_values(self, indexer):
926926
# GH#10043 this is fundamentally a test for iloc, but test loc while
927927
# we're here

pandas/tests/series/indexing/test_setitem.py

Lines changed: 82 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,88 @@ def test_setitem_callable_other(self):
242242
tm.assert_series_equal(ser, expected)
243243

244244

245+
class TestSetitemWithExpansion:
246+
def test_setitem_empty_series(self):
247+
# GH#10193
248+
key = Timestamp("2012-01-01")
249+
series = Series(dtype=object)
250+
series[key] = 47
251+
expected = Series(47, [key])
252+
tm.assert_series_equal(series, expected)
253+
254+
def test_setitem_empty_series_datetimeindex_preserves_freq(self):
255+
# GH#33573 our index should retain its freq
256+
series = Series([], DatetimeIndex([], freq="D"), dtype=object)
257+
key = Timestamp("2012-01-01")
258+
series[key] = 47
259+
expected = Series(47, DatetimeIndex([key], freq="D"))
260+
tm.assert_series_equal(series, expected)
261+
assert series.index.freq == expected.index.freq
262+
263+
def test_setitem_empty_series_timestamp_preserves_dtype(self):
264+
# GH 21881
265+
timestamp = Timestamp(1412526600000000000)
266+
series = Series([timestamp], index=["timestamp"], dtype=object)
267+
expected = series["timestamp"]
268+
269+
series = Series([], dtype=object)
270+
series["anything"] = 300.0
271+
series["timestamp"] = timestamp
272+
result = series["timestamp"]
273+
assert result == expected
274+
275+
@pytest.mark.parametrize(
276+
"td",
277+
[
278+
Timedelta("9 days"),
279+
Timedelta("9 days").to_timedelta64(),
280+
Timedelta("9 days").to_pytimedelta(),
281+
],
282+
)
283+
def test_append_timedelta_does_not_cast(self, td):
284+
# GH#22717 inserting a Timedelta should _not_ cast to int64
285+
expected = Series(["x", td], index=[0, "td"], dtype=object)
286+
287+
ser = Series(["x"])
288+
ser["td"] = td
289+
tm.assert_series_equal(ser, expected)
290+
assert isinstance(ser["td"], Timedelta)
291+
292+
ser = Series(["x"])
293+
ser.loc["td"] = Timedelta("9 days")
294+
tm.assert_series_equal(ser, expected)
295+
assert isinstance(ser["td"], Timedelta)
296+
297+
298+
def test_setitem_scalar_into_readonly_backing_data():
299+
# GH#14359: test that you cannot mutate a read only buffer
300+
301+
array = np.zeros(5)
302+
array.flags.writeable = False # make the array immutable
303+
series = Series(array)
304+
305+
for n in range(len(series)):
306+
msg = "assignment destination is read-only"
307+
with pytest.raises(ValueError, match=msg):
308+
series[n] = 1
309+
310+
assert array[n] == 0
311+
312+
313+
def test_setitem_slice_into_readonly_backing_data():
314+
# GH#14359: test that you cannot mutate a read only buffer
315+
316+
array = np.zeros(5)
317+
array.flags.writeable = False # make the array immutable
318+
series = Series(array)
319+
320+
msg = "assignment destination is read-only"
321+
with pytest.raises(ValueError, match=msg):
322+
series[1:3] = 1
323+
324+
assert not array.any()
325+
326+
245327
class TestSetitemCasting:
246328
@pytest.mark.parametrize("unique", [True, False])
247329
@pytest.mark.parametrize("val", [3, 3.0, "3"], ids=type)
@@ -445,88 +527,6 @@ def val(self, request):
445527
return request.param
446528

447529

448-
class TestSetitemWithExpansion:
449-
def test_setitem_empty_series(self):
450-
# GH#10193
451-
key = Timestamp("2012-01-01")
452-
series = Series(dtype=object)
453-
series[key] = 47
454-
expected = Series(47, [key])
455-
tm.assert_series_equal(series, expected)
456-
457-
def test_setitem_empty_series_datetimeindex_preserves_freq(self):
458-
# GH#33573 our index should retain its freq
459-
series = Series([], DatetimeIndex([], freq="D"), dtype=object)
460-
key = Timestamp("2012-01-01")
461-
series[key] = 47
462-
expected = Series(47, DatetimeIndex([key], freq="D"))
463-
tm.assert_series_equal(series, expected)
464-
assert series.index.freq == expected.index.freq
465-
466-
def test_setitem_empty_series_timestamp_preserves_dtype(self):
467-
# GH 21881
468-
timestamp = Timestamp(1412526600000000000)
469-
series = Series([timestamp], index=["timestamp"], dtype=object)
470-
expected = series["timestamp"]
471-
472-
series = Series([], dtype=object)
473-
series["anything"] = 300.0
474-
series["timestamp"] = timestamp
475-
result = series["timestamp"]
476-
assert result == expected
477-
478-
@pytest.mark.parametrize(
479-
"td",
480-
[
481-
Timedelta("9 days"),
482-
Timedelta("9 days").to_timedelta64(),
483-
Timedelta("9 days").to_pytimedelta(),
484-
],
485-
)
486-
def test_append_timedelta_does_not_cast(self, td):
487-
# GH#22717 inserting a Timedelta should _not_ cast to int64
488-
expected = Series(["x", td], index=[0, "td"], dtype=object)
489-
490-
ser = Series(["x"])
491-
ser["td"] = td
492-
tm.assert_series_equal(ser, expected)
493-
assert isinstance(ser["td"], Timedelta)
494-
495-
ser = Series(["x"])
496-
ser.loc["td"] = Timedelta("9 days")
497-
tm.assert_series_equal(ser, expected)
498-
assert isinstance(ser["td"], Timedelta)
499-
500-
501-
def test_setitem_scalar_into_readonly_backing_data():
502-
# GH#14359: test that you cannot mutate a read only buffer
503-
504-
array = np.zeros(5)
505-
array.flags.writeable = False # make the array immutable
506-
series = Series(array)
507-
508-
for n in range(len(series)):
509-
msg = "assignment destination is read-only"
510-
with pytest.raises(ValueError, match=msg):
511-
series[n] = 1
512-
513-
assert array[n] == 0
514-
515-
516-
def test_setitem_slice_into_readonly_backing_data():
517-
# GH#14359: test that you cannot mutate a read only buffer
518-
519-
array = np.zeros(5)
520-
array.flags.writeable = False # make the array immutable
521-
series = Series(array)
522-
523-
msg = "assignment destination is read-only"
524-
with pytest.raises(ValueError, match=msg):
525-
series[1:3] = 1
526-
527-
assert not array.any()
528-
529-
530530
class TestSetitemTimedelta64IntoNumeric(SetitemCastingEquivalents):
531531
# timedelta64 should not be treated as integers when setting into
532532
# numeric Series

0 commit comments

Comments
 (0)