Skip to content

CLN: Address some TODO comments #23208

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 4 commits into from
Oct 19, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
26 changes: 0 additions & 26 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,6 @@ def consensus_name_attr(objs):
return name


# TODO: only used once in frame.py; belongs elsewhere?
def get_info_slice(obj, indexer):
"""Slice the info axis of `obj` with `indexer`."""
if not hasattr(obj, '_info_axis_number'):
msg = 'object of type {typ!r} has no info axis'
raise TypeError(msg.format(typ=type(obj).__name__))
slices = [slice(None)] * obj.ndim
slices[obj._info_axis_number] = indexer
return tuple(slices)


def maybe_box(indexer, values, obj, key):

# if we have multiples coming back, box em
Expand Down Expand Up @@ -432,21 +421,6 @@ def random_state(state=None):
"RandomState, or None")


# TODO: only used once in indexes.api; belongs elsewhere?
def get_distinct_objs(objs):
"""
Return a list with distinct elements of "objs" (different ids).
Preserves order.
"""
ids = set()
res = []
for obj in objs:
if not id(obj) in ids:
ids.add(id(obj))
res.append(obj)
return res


def _pipe(obj, func, *args, **kwargs):
"""
Apply a function ``func`` to object ``obj`` either by passing obj as the
Expand Down
12 changes: 11 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3205,7 +3205,7 @@ def is_dtype_instance_mapper(idx, dtype):
exclude_these.iloc[idx] = not any(map(f, exclude))

dtype_indexer = include_these & exclude_these
return self.loc[com.get_info_slice(self, dtype_indexer)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just move the local function here

return self.loc[_get_info_slice(self, dtype_indexer)]

def _box_item_values(self, key, values):
items = self.columns[self.columns.get_loc(key)]
Expand Down Expand Up @@ -8040,3 +8040,13 @@ def _from_nested_dict(data):

def _put_str(s, space):
return u'{s}'.format(s=s)[:space].ljust(space)


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't put this here, pandas/core/indexing.py is better or pandas/core/index/api.py

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_get_info_slice is only used once in frame.py and nowhere else. I was thinking we might as well avoid the import.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, can you just inline it then where it is used (make it a local function) instead.

def _get_info_slice(obj, indexer):
"""Slice the info axis of `obj` with `indexer`."""
if not hasattr(obj, '_info_axis_number'):
msg = 'object of type {typ!r} has no info axis'
raise TypeError(msg.format(typ=type(obj).__name__))
slices = [slice(None)] * obj.ndim
slices[obj._info_axis_number] = indexer
return tuple(slices)
10 changes: 0 additions & 10 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4069,16 +4069,6 @@ def _reindex_with_indexers(self, reindexers, fill_value=None, copy=False,

return self._constructor(new_data).__finalize__(self)

# TODO: unused; remove?
def _reindex_axis(self, new_index, fill_method, axis, copy):
new_data = self._data.reindex_axis(new_index, axis=axis,
method=fill_method, copy=copy)

if new_data is self._data and not copy:
return self
else:
return self._constructor(new_data).__finalize__(self)

def filter(self, items=None, like=None, regex=None, axis=None):
"""
Subset rows or columns of dataframe according to labels in
Expand Down
16 changes: 15 additions & 1 deletion pandas/core/indexes/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,23 @@ def _get_objs_combined_axis(objs, intersect=False, axis=0, sort=True):
return _get_combined_index(obs_idxes, intersect=intersect, sort=sort)


def _get_distinct_objs(objs):
"""
Return a list with distinct elements of "objs" (different ids).
Preserves order.
"""
ids = set()
res = []
for obj in objs:
if not id(obj) in ids:
ids.add(id(obj))
res.append(obj)
return res


def _get_combined_index(indexes, intersect=False, sort=False):
# TODO: handle index names!
indexes = com.get_distinct_objs(indexes)
indexes = _get_distinct_objs(indexes)
if len(indexes) == 0:
index = Index([])
elif len(indexes) == 1:
Expand Down
46 changes: 3 additions & 43 deletions pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,60 +511,20 @@ def _maybe_cast_slice_bound(self, label, side, kind):

return label

def _get_string_slice(self, key, use_lhs=True, use_rhs=True):
freq = getattr(self, 'freqstr',
getattr(self, 'inferred_freq', None))
def _get_string_slice(self, key):
if is_integer(key) or is_float(key) or key is NaT:
self._invalid_indexer('slice', key)
loc = self._partial_td_slice(key, freq, use_lhs=use_lhs,
use_rhs=use_rhs)
loc = self._partial_td_slice(key)
return loc

def _partial_td_slice(self, key, freq, use_lhs=True, use_rhs=True):
def _partial_td_slice(self, key):

# given a key, try to figure out a location for a partial slice
if not isinstance(key, compat.string_types):
return key

raise NotImplementedError

# TODO(wesm): dead code
# parsed = _coerce_scalar_to_timedelta_type(key, box=True)

# is_monotonic = self.is_monotonic

# # figure out the resolution of the passed td
# # and round to it

# # t1 = parsed.round(reso)

# t2 = t1 + to_offset(parsed.resolution) - Timedelta(1, 'ns')

# stamps = self.asi8

# if is_monotonic:

# # we are out of range
# if (len(stamps) and ((use_lhs and t1.value < stamps[0] and
# t2.value < stamps[0]) or
# ((use_rhs and t1.value > stamps[-1] and
# t2.value > stamps[-1])))):
# raise KeyError

# # a monotonic (sorted) series can be sliced
# left = (stamps.searchsorted(t1.value, side='left')
# if use_lhs else None)
# right = (stamps.searchsorted(t2.value, side='right')
# if use_rhs else None)

# return slice(left, right)

# lhs_mask = (stamps >= t1.value) if use_lhs else True
# rhs_mask = (stamps <= t2.value) if use_rhs else True

# # try to find a the dates
# return (lhs_mask & rhs_mask).nonzero()[0]

@Substitution(klass='TimedeltaIndex')
@Appender(_shared_docs['searchsorted'])
def searchsorted(self, value, side='left', sorter=None):
Expand Down
4 changes: 1 addition & 3 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2812,9 +2812,7 @@ def _astype(self, dtype, mgr=None, **kwargs):
def _can_hold_element(self, element):
tipo = maybe_infer_dtype_type(element)
if tipo is not None:
# TODO: this still uses asarray, instead of dtype.type
element = np.array(element)
return element.dtype == _NS_DTYPE or element.dtype == np.int64
return tipo == _NS_DTYPE or tipo == np.int64
return (is_integer(element) or isinstance(element, datetime) or
isna(element))

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/test_mutate_columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def test_pop(self):
self.frame['foo'] = 'bar'
self.frame.pop('foo')
assert 'foo' not in self.frame
# TODO assert self.frame.columns.name == 'baz'
assert self.frame.columns.name == 'baz'

# gh-10912: inplace ops cause caching issue
a = DataFrame([[1, 2, 3], [4, 5, 6]], columns=[
Expand Down