Skip to content

CLN: assorted cleanups #39856

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Feb 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,3 @@ doc:
cd doc; \
python make.py clean; \
python make.py html

check:
python3 scripts/validate_unwanted_patterns.py \
--validation-type="private_function_across_module" \
--included-file-extensions="py" \
--excluded-file-paths=pandas/tests,asv_bench/ \
pandas/

python3 scripts/validate_unwanted_patterns.py \
--validation-type="private_import_across_module" \
--included-file-extensions="py" \
--excluded-file-paths=pandas/tests,asv_bench/,doc/
pandas/
6 changes: 1 addition & 5 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,7 @@
ABCSeries,
)
from pandas.core.dtypes.inference import iterable_not_string
from pandas.core.dtypes.missing import ( # noqa
isna,
isnull,
notnull,
)
from pandas.core.dtypes.missing import isna


class SettingWithCopyError(ValueError):
Expand Down
7 changes: 4 additions & 3 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -752,10 +752,11 @@ def _can_fast_transpose(self) -> bool:
"""
if isinstance(self._mgr, ArrayManager):
return False
if self._mgr.any_extension_types:
# TODO(EA2D) special case would be unnecessary with 2D EAs
blocks = self._mgr.blocks
if len(blocks) != 1:
return False
return len(self._mgr.blocks) == 1

return not self._mgr.any_extension_types

# ----------------------------------------------------------------------
# Rendering Methods
Expand Down
4 changes: 1 addition & 3 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -822,9 +822,7 @@ def _convert_list_indexer(self, keyarr):
def _is_comparable_dtype(self, dtype: DtypeObj) -> bool:
if not isinstance(dtype, IntervalDtype):
return False
if self.closed != dtype.closed:
return False
common_subtype = find_common_type([self.dtype.subtype, dtype.subtype])
common_subtype = find_common_type([self.dtype, dtype])
return not is_object_dtype(common_subtype)

# --------------------------------------------------------------------
Expand Down
16 changes: 7 additions & 9 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,7 @@ def setitem(self, indexer, value):
arr_value = value
else:
is_ea_value = False
arr_value = np.array(value)
arr_value = np.asarray(value)

if transpose:
values = values.T
Expand Down Expand Up @@ -1640,8 +1640,8 @@ def setitem(self, indexer, value):
be a compatible shape.
"""
if not self._can_hold_element(value):
# This is only relevant for DatetimeTZBlock, which has a
# non-trivial `_can_hold_element`.
# This is only relevant for DatetimeTZBlock, ObjectValuesExtensionBlock,
# which has a non-trivial `_can_hold_element`.
# https://github.com/pandas-dev/pandas/issues/24020
# Need a dedicated setitem until GH#24020 (type promotion in setitem
# for extension arrays) is designed and implemented.
Expand Down Expand Up @@ -2052,6 +2052,8 @@ def shift(self, periods: int, axis: int = 0, fill_value: Any = None) -> List[Blo
class DatetimeLikeBlockMixin(NDArrayBackedExtensionBlock):
"""Mixin class for DatetimeBlock, DatetimeTZBlock, and TimedeltaBlock."""

is_numeric = False
_can_hold_na = True
_dtype: np.dtype
_holder: Type[Union[DatetimeArray, TimedeltaArray]]

Expand Down Expand Up @@ -2104,10 +2106,6 @@ class DatetimeBlock(DatetimeLikeBlockMixin):
_dtype = fill_value.dtype
_holder = DatetimeArray

@property
def _can_hold_na(self):
return True

def set_inplace(self, locs, values):
"""
See Block.set.__doc__
Expand All @@ -2124,6 +2122,8 @@ class DatetimeTZBlock(ExtensionBlock, DatetimeBlock):

__slots__ = ()
is_extension = True
_can_hold_na = True
is_numeric = False

_holder = DatetimeArray

Expand Down Expand Up @@ -2188,8 +2188,6 @@ def fillna(

class TimeDeltaBlock(DatetimeLikeBlockMixin):
__slots__ = ()
_can_hold_na = True
is_numeric = False
_holder = TimedeltaArray
fill_value = np.timedelta64("NaT", "ns")
_dtype = fill_value.dtype
Expand Down
7 changes: 2 additions & 5 deletions pandas/core/internals/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,8 @@ def concatenate_block_managers(
else:
b = make_block(values, placement=placement, ndim=blk.ndim)
else:
b = make_block(
_concatenate_join_units(join_units, concat_axis, copy=copy),
placement=placement,
ndim=len(axes),
)
new_values = _concatenate_join_units(join_units, concat_axis, copy=copy)
b = make_block(new_values, placement=placement, ndim=len(axes))
blocks.append(b)

return BlockManager(blocks, axes)
Expand Down
9 changes: 5 additions & 4 deletions pandas/core/reshape/reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ def unstack(obj, level, fill_value=None):
return obj.T.stack(dropna=False)
elif not isinstance(obj.index, MultiIndex):
# GH 36113
# Give nicer error messages when unstack a Series whose
# Give nicer error messages when unstack a Series whose
# Index is not a MultiIndex.
raise ValueError(
f"index must be a MultiIndex to unstack, {type(obj.index)} was passed"
Expand All @@ -451,9 +451,10 @@ def _unstack_frame(obj, level, fill_value=None):
mgr = obj._mgr.unstack(unstacker, fill_value=fill_value)
return obj._constructor(mgr)
else:
return _Unstacker(
obj.index, level=level, constructor=obj._constructor
).get_result(obj._values, value_columns=obj.columns, fill_value=fill_value)
unstacker = _Unstacker(obj.index, level=level, constructor=obj._constructor)
return unstacker.get_result(
obj._values, value_columns=obj.columns, fill_value=fill_value
)


def _unstack_extension_series(series, level, fill_value):
Expand Down
1 change: 1 addition & 0 deletions pandas/tests/arithmetic/test_timedelta64.py
Original file line number Diff line number Diff line change
Expand Up @@ -2090,6 +2090,7 @@ def test_td64arr_div_numeric_array(
expected = [tdser[n] / vector[n] for n in range(len(tdser))]
expected = pd.Index(expected) # do dtype inference
expected = tm.box_expected(expected, xbox)
assert tm.get_dtype(expected) == "m8[ns]"

if using_array_manager and box_with_array is pd.DataFrame:
# TODO the behaviour is buggy here (third column with all-NaT
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexing/test_iloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,7 @@ def test_iloc_setitem_empty_frame_raises_with_3d_ndarray(self):
with pytest.raises(ValueError, match=msg):
obj.iloc[nd3] = 0

@pytest.mark.parametrize("indexer", [lambda x: x.loc, lambda x: x.iloc])
@pytest.mark.parametrize("indexer", [tm.loc, tm.iloc])
def test_iloc_getitem_read_only_values(self, indexer):
# GH#10043 this is fundamentally a test for iloc, but test loc while
# we're here
Expand Down
164 changes: 82 additions & 82 deletions pandas/tests/series/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,88 @@ def test_setitem_callable_other(self):
tm.assert_series_equal(ser, expected)


class TestSetitemWithExpansion:
def test_setitem_empty_series(self):
# GH#10193
key = Timestamp("2012-01-01")
series = Series(dtype=object)
series[key] = 47
expected = Series(47, [key])
tm.assert_series_equal(series, expected)

def test_setitem_empty_series_datetimeindex_preserves_freq(self):
# GH#33573 our index should retain its freq
series = Series([], DatetimeIndex([], freq="D"), dtype=object)
key = Timestamp("2012-01-01")
series[key] = 47
expected = Series(47, DatetimeIndex([key], freq="D"))
tm.assert_series_equal(series, expected)
assert series.index.freq == expected.index.freq

def test_setitem_empty_series_timestamp_preserves_dtype(self):
# GH 21881
timestamp = Timestamp(1412526600000000000)
series = Series([timestamp], index=["timestamp"], dtype=object)
expected = series["timestamp"]

series = Series([], dtype=object)
series["anything"] = 300.0
series["timestamp"] = timestamp
result = series["timestamp"]
assert result == expected

@pytest.mark.parametrize(
"td",
[
Timedelta("9 days"),
Timedelta("9 days").to_timedelta64(),
Timedelta("9 days").to_pytimedelta(),
],
)
def test_append_timedelta_does_not_cast(self, td):
# GH#22717 inserting a Timedelta should _not_ cast to int64
expected = Series(["x", td], index=[0, "td"], dtype=object)

ser = Series(["x"])
ser["td"] = td
tm.assert_series_equal(ser, expected)
assert isinstance(ser["td"], Timedelta)

ser = Series(["x"])
ser.loc["td"] = Timedelta("9 days")
tm.assert_series_equal(ser, expected)
assert isinstance(ser["td"], Timedelta)


def test_setitem_scalar_into_readonly_backing_data():
# GH#14359: test that you cannot mutate a read only buffer

array = np.zeros(5)
array.flags.writeable = False # make the array immutable
series = Series(array)

for n in range(len(series)):
msg = "assignment destination is read-only"
with pytest.raises(ValueError, match=msg):
series[n] = 1

assert array[n] == 0


def test_setitem_slice_into_readonly_backing_data():
# GH#14359: test that you cannot mutate a read only buffer

array = np.zeros(5)
array.flags.writeable = False # make the array immutable
series = Series(array)

msg = "assignment destination is read-only"
with pytest.raises(ValueError, match=msg):
series[1:3] = 1

assert not array.any()


class TestSetitemCasting:
@pytest.mark.parametrize("unique", [True, False])
@pytest.mark.parametrize("val", [3, 3.0, "3"], ids=type)
Expand Down Expand Up @@ -445,88 +527,6 @@ def val(self, request):
return request.param


class TestSetitemWithExpansion:
def test_setitem_empty_series(self):
# GH#10193
key = Timestamp("2012-01-01")
series = Series(dtype=object)
series[key] = 47
expected = Series(47, [key])
tm.assert_series_equal(series, expected)

def test_setitem_empty_series_datetimeindex_preserves_freq(self):
# GH#33573 our index should retain its freq
series = Series([], DatetimeIndex([], freq="D"), dtype=object)
key = Timestamp("2012-01-01")
series[key] = 47
expected = Series(47, DatetimeIndex([key], freq="D"))
tm.assert_series_equal(series, expected)
assert series.index.freq == expected.index.freq

def test_setitem_empty_series_timestamp_preserves_dtype(self):
# GH 21881
timestamp = Timestamp(1412526600000000000)
series = Series([timestamp], index=["timestamp"], dtype=object)
expected = series["timestamp"]

series = Series([], dtype=object)
series["anything"] = 300.0
series["timestamp"] = timestamp
result = series["timestamp"]
assert result == expected

@pytest.mark.parametrize(
"td",
[
Timedelta("9 days"),
Timedelta("9 days").to_timedelta64(),
Timedelta("9 days").to_pytimedelta(),
],
)
def test_append_timedelta_does_not_cast(self, td):
# GH#22717 inserting a Timedelta should _not_ cast to int64
expected = Series(["x", td], index=[0, "td"], dtype=object)

ser = Series(["x"])
ser["td"] = td
tm.assert_series_equal(ser, expected)
assert isinstance(ser["td"], Timedelta)

ser = Series(["x"])
ser.loc["td"] = Timedelta("9 days")
tm.assert_series_equal(ser, expected)
assert isinstance(ser["td"], Timedelta)


def test_setitem_scalar_into_readonly_backing_data():
# GH#14359: test that you cannot mutate a read only buffer

array = np.zeros(5)
array.flags.writeable = False # make the array immutable
series = Series(array)

for n in range(len(series)):
msg = "assignment destination is read-only"
with pytest.raises(ValueError, match=msg):
series[n] = 1

assert array[n] == 0


def test_setitem_slice_into_readonly_backing_data():
# GH#14359: test that you cannot mutate a read only buffer

array = np.zeros(5)
array.flags.writeable = False # make the array immutable
series = Series(array)

msg = "assignment destination is read-only"
with pytest.raises(ValueError, match=msg):
series[1:3] = 1

assert not array.any()


class TestSetitemTimedelta64IntoNumeric(SetitemCastingEquivalents):
# timedelta64 should not be treated as integers when setting into
# numeric Series
Expand Down