Skip to content

CLN: collected cleanups from other branches #27723

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 5 commits into from
Aug 5, 2019
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
4 changes: 0 additions & 4 deletions pandas/_libs/index.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ cpdef get_value_at(ndarray arr, object loc, object tz=None):
return util.get_value_at(arr, loc)


def get_value_box(arr: ndarray, loc: object) -> object:
return get_value_at(arr, loc, tz=None)


# Don't populate hash tables in monotonic indexes larger than this
_SIZE_CUTOFF = 1000000

Expand Down
6 changes: 5 additions & 1 deletion pandas/core/arrays/numpy_.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ def __init__(self, values, copy=False):
if isinstance(values, type(self)):
values = values._ndarray
if not isinstance(values, np.ndarray):
raise ValueError("'values' must be a NumPy array.")
raise ValueError(
"'values' must be a NumPy array, not {typ}".format(
typ=type(values).__name__
)
)

if values.ndim != 1:
raise ValueError("PandasArray must be 1-dimensional.")
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ class TimedeltaArray(dtl.DatetimeLikeArrayMixin, dtl.TimelikeOps):
"ceil",
]

# Needed so that NaT.__richcmp__(DateTimeArray) operates pointwise
ndim = 1
# Note: ndim must be defined to ensure NaT.__richcmp(TimedeltaArray)
# operates pointwise.

@property
def _box_func(self):
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4713,7 +4713,7 @@ def get_value(self, series, key):
raise

try:
return libindex.get_value_box(s, key)
return libindex.get_value_at(s, key)
except IndexError:
raise
except TypeError:
Expand Down
54 changes: 38 additions & 16 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ def f(m, v, i):

return self.split_and_operate(mask, f, inplace)

def split_and_operate(self, mask, f, inplace):
def split_and_operate(self, mask, f, inplace: bool):
"""
split the block per-column, and apply the callable f
per-column, return a new block for each. Handle
Expand Down Expand Up @@ -493,17 +493,15 @@ def make_a_block(nv, ref_loc):

return new_blocks

def _maybe_downcast(self, blocks, downcast=None):
def _maybe_downcast(self, blocks: List["Block"], downcast=None) -> List["Block"]:

# no need to downcast our float
# unless indicated
if downcast is None and self.is_float:
return blocks
elif downcast is None and (self.is_timedelta or self.is_datetime):
if downcast is None and (
self.is_float or self.is_timedelta or self.is_datetime
):
return blocks

if not isinstance(blocks, list):
blocks = [blocks]
return _extend_blocks([b.downcast(downcast) for b in blocks])

def downcast(self, dtypes=None):
Expand Down Expand Up @@ -1343,7 +1341,15 @@ def shift(self, periods, axis=0, fill_value=None):

return [self.make_block(new_values)]

def where(self, other, cond, align=True, errors="raise", try_cast=False, axis=0):
def where(
self,
other,
cond,
align=True,
errors="raise",
try_cast: bool = False,
axis: int = 0,
) -> List["Block"]:
"""
evaluate the block; return result block(s) from the result

Expand Down Expand Up @@ -1442,7 +1448,7 @@ def func(cond, values, other):
if try_cast:
result = self._try_cast_result(result)

return self.make_block(result)
return [self.make_block(result)]

# might need to separate out blocks
axis = cond.ndim - 1
Expand Down Expand Up @@ -1474,9 +1480,9 @@ def _unstack(self, unstacker_func, new_columns, n_rows, fill_value):
new_columns : Index
All columns of the unstacked BlockManager.
n_rows : int
Only used in ExtensionBlock.unstack
Only used in ExtensionBlock._unstack
fill_value : int
Only used in ExtensionBlock.unstack
Only used in ExtensionBlock._unstack

Returns
-------
Expand Down Expand Up @@ -1550,7 +1556,7 @@ def quantile(self, qs, interpolation="linear", axis=0):
result = result[..., 0]
result = lib.item_from_zerodim(result)

ndim = getattr(result, "ndim", None) or 0
ndim = np.ndim(result)
return make_block(result, placement=np.arange(len(result)), ndim=ndim)

def _replace_coerce(
Expand Down Expand Up @@ -1923,7 +1929,15 @@ def shift(
)
]

def where(self, other, cond, align=True, errors="raise", try_cast=False, axis=0):
def where(
self,
other,
cond,
align=True,
errors="raise",
try_cast: bool = False,
axis: int = 0,
) -> List["Block"]:
if isinstance(other, ABCDataFrame):
# ExtensionArrays are 1-D, so if we get here then
# `other` should be a DataFrame with a single column.
Expand Down Expand Up @@ -1968,7 +1982,7 @@ def where(self, other, cond, align=True, errors="raise", try_cast=False, axis=0)
np.where(cond, self.values, other), dtype=dtype
)

return self.make_block_same_class(result, placement=self.mgr_locs)
return [self.make_block_same_class(result, placement=self.mgr_locs)]

@property
def _ftype(self):
Expand Down Expand Up @@ -2706,7 +2720,7 @@ def f(m, v, i):

return blocks

def _maybe_downcast(self, blocks, downcast=None):
def _maybe_downcast(self, blocks: List["Block"], downcast=None) -> List["Block"]:

if downcast is not None:
return blocks
Expand Down Expand Up @@ -3031,7 +3045,15 @@ def concat_same_type(self, to_concat, placement=None):
values, placement=placement or slice(0, len(values), 1), ndim=self.ndim
)

def where(self, other, cond, align=True, errors="raise", try_cast=False, axis=0):
def where(
self,
other,
cond,
align=True,
errors="raise",
try_cast: bool = False,
axis: int = 0,
) -> List["Block"]:
# TODO(CategoricalBlock.where):
# This can all be deleted in favor of ExtensionBlock.where once
# we enforce the deprecation.
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1823,7 +1823,7 @@ def _simple_blockify(tuples, dtype):
"""
values, placement = _stack_arrays(tuples, dtype)

# CHECK DTYPE?
# TODO: CHECK DTYPE?
if dtype is not None and values.dtype != dtype: # pragma: no cover
values = values.astype(dtype)

Expand Down
9 changes: 4 additions & 5 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -1630,15 +1630,14 @@ def _get_period_bins(self, ax):


def _take_new_index(obj, indexer, new_index, axis=0):
from pandas.core.api import Series, DataFrame

if isinstance(obj, Series):
if isinstance(obj, ABCSeries):
new_values = algos.take_1d(obj.values, indexer)
return Series(new_values, index=new_index, name=obj.name)
elif isinstance(obj, DataFrame):
return obj._constructor(new_values, index=new_index, name=obj.name)
elif isinstance(obj, ABCDataFrame):
if axis == 1:
raise NotImplementedError("axis 1 is not supported")
return DataFrame(
return obj._constructor(
obj._data.reindex_indexer(new_axis=new_index, indexer=indexer, axis=1)
)
else:
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ def _wrap_result(self, result, block=None, obj=None):
# coerce if necessary
if block is not None:
if is_timedelta64_dtype(block.values.dtype):
# TODO: do we know what result.dtype is at this point?
# i.e. can we just do an astype?
from pandas import to_timedelta

result = to_timedelta(result.ravel(), unit="ns").values.reshape(
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/groupby/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ def test_datetime():
desc_result = grouped.describe()

idx = cats.codes.argsort()
ord_labels = cats.take_nd(idx)
ord_labels = cats.take(idx)
ord_data = data.take(idx)
expected = ord_data.groupby(ord_labels, observed=False).describe()
assert_frame_equal(desc_result, expected)
Expand Down