Skip to content

Change ._data to ._parent for accessors #21906

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 9 commits into from
Aug 8, 2018
10 changes: 5 additions & 5 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -2401,7 +2401,7 @@ class CategoricalAccessor(PandasDelegate, PandasObject, NoNewAttributesMixin):

def __init__(self, data):
self._validate(data)
self.categorical = data.values
self._parent = data.values
self.index = data.index
self.name = data.name
self._freeze()
Expand All @@ -2413,19 +2413,19 @@ def _validate(data):
"'category' dtype")

def _delegate_property_get(self, name):
return getattr(self.categorical, name)
return getattr(self._parent, name)

def _delegate_property_set(self, name, new_values):
return setattr(self.categorical, name, new_values)
return setattr(self._parent, name, new_values)

@property
def codes(self):
from pandas import Series
return Series(self.categorical.codes, index=self.index)
return Series(self._parent.codes, index=self.index)

def _delegate_method(self, name, *args, **kwargs):
from pandas import Series
method = getattr(self.categorical, name)
method = getattr(self._parent, name)
res = method(*args, **kwargs)
if res is not None:
return Series(res, index=self.index, name=self.name)
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ def __init__(self, data, orig):
raise TypeError("cannot convert an object of type {0} to a "
"datetimelike index".format(type(data)))

self.values = data
Copy link
Contributor

Choose a reason for hiding this comment

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

am puzzled why this doesn't actually break anything; i think that the dateitmelike accessors do something different?

Copy link
Member Author

Choose a reason for hiding this comment

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

i think that the dateitmelike accessors do something different?

CategoricalAccessor uses self.categories, is the outlier this PR leaves unchanged.

self._parent = data
self.orig = orig
self.name = getattr(data, 'name', None)
self.index = getattr(data, 'index', None)
self._freeze()

def _get_values(self):
data = self.values
data = self._parent
if is_datetime64_dtype(data.dtype):
return DatetimeIndex(data, copy=False, name=self.name)

Expand Down
69 changes: 36 additions & 33 deletions pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@ def str_extract(arr, pat, flags=0, expand=True):
if expand:
return _str_extract_frame(arr._orig, pat, flags=flags)
else:
result, name = _str_extract_noexpand(arr._data, pat, flags=flags)
result, name = _str_extract_noexpand(arr._parent, pat, flags=flags)
return arr._wrap_result(result, name=name, expand=expand)


Expand Down Expand Up @@ -1721,7 +1721,7 @@ def str_encode(arr, encoding, errors="strict"):

def _noarg_wrapper(f, docstring=None, **kargs):
def wrapper(self):
result = _na_map(f, self._data, **kargs)
result = _na_map(f, self._parent, **kargs)
return self._wrap_result(result)

wrapper.__name__ = f.__name__
Expand All @@ -1735,15 +1735,15 @@ def wrapper(self):

def _pat_wrapper(f, flags=False, na=False, **kwargs):
def wrapper1(self, pat):
result = f(self._data, pat)
result = f(self._parent, pat)
return self._wrap_result(result)

def wrapper2(self, pat, flags=0, **kwargs):
result = f(self._data, pat, flags=flags, **kwargs)
result = f(self._parent, pat, flags=flags, **kwargs)
return self._wrap_result(result)

def wrapper3(self, pat, na=np.nan):
result = f(self._data, pat, na=na)
result = f(self._parent, pat, na=na)
return self._wrap_result(result)

wrapper = wrapper3 if na else wrapper2 if flags else wrapper1
Expand Down Expand Up @@ -1783,7 +1783,7 @@ def __init__(self, data):
self._is_categorical = is_categorical_dtype(data)

# .values.categories works for both Series/Index
self._data = data.values.categories if self._is_categorical else data
self._parent = data.values.categories if self._is_categorical else data
# save orig to blow up categoricals to the right type
self._orig = data
self._freeze()
Expand Down Expand Up @@ -2334,14 +2334,14 @@ def cat(self, others=None, sep=None, na_rep=None, join=None):
'side': 'beginning',
'method': 'split'})
def split(self, pat=None, n=-1, expand=False):
result = str_split(self._data, pat, n=n)
result = str_split(self._parent, pat, n=n)
return self._wrap_result(result, expand=expand)

@Appender(_shared_docs['str_split'] % {
'side': 'end',
'method': 'rsplit'})
def rsplit(self, pat=None, n=-1, expand=False):
result = str_rsplit(self._data, pat, n=n)
result = str_rsplit(self._parent, pat, n=n)
return self._wrap_result(result, expand=expand)

_shared_docs['str_partition'] = ("""
Expand Down Expand Up @@ -2432,7 +2432,7 @@ def rsplit(self, pat=None, n=-1, expand=False):
})
def partition(self, pat=' ', expand=True):
f = lambda x: x.partition(pat)
result = _na_map(f, self._data)
result = _na_map(f, self._parent)
return self._wrap_result(result, expand=expand)

@Appender(_shared_docs['str_partition'] % {
Expand All @@ -2443,45 +2443,45 @@ def partition(self, pat=' ', expand=True):
})
def rpartition(self, pat=' ', expand=True):
f = lambda x: x.rpartition(pat)
result = _na_map(f, self._data)
result = _na_map(f, self._parent)
return self._wrap_result(result, expand=expand)

@copy(str_get)
def get(self, i):
result = str_get(self._data, i)
result = str_get(self._parent, i)
return self._wrap_result(result)

@copy(str_join)
def join(self, sep):
result = str_join(self._data, sep)
result = str_join(self._parent, sep)
return self._wrap_result(result)

@copy(str_contains)
def contains(self, pat, case=True, flags=0, na=np.nan, regex=True):
result = str_contains(self._data, pat, case=case, flags=flags, na=na,
result = str_contains(self._parent, pat, case=case, flags=flags, na=na,
regex=regex)
return self._wrap_result(result)

@copy(str_match)
def match(self, pat, case=True, flags=0, na=np.nan, as_indexer=None):
result = str_match(self._data, pat, case=case, flags=flags, na=na,
result = str_match(self._parent, pat, case=case, flags=flags, na=na,
as_indexer=as_indexer)
return self._wrap_result(result)

@copy(str_replace)
def replace(self, pat, repl, n=-1, case=None, flags=0, regex=True):
result = str_replace(self._data, pat, repl, n=n, case=case,
result = str_replace(self._parent, pat, repl, n=n, case=case,
flags=flags, regex=regex)
return self._wrap_result(result)

@copy(str_repeat)
def repeat(self, repeats):
result = str_repeat(self._data, repeats)
result = str_repeat(self._parent, repeats)
return self._wrap_result(result)

@copy(str_pad)
def pad(self, width, side='left', fillchar=' '):
result = str_pad(self._data, width, side=side, fillchar=fillchar)
result = str_pad(self._parent, width, side=side, fillchar=fillchar)
return self._wrap_result(result)

_shared_docs['str_pad'] = ("""
Expand Down Expand Up @@ -2574,27 +2574,27 @@ def zfill(self, width):
4 NaN
dtype: object
"""
result = str_pad(self._data, width, side='left', fillchar='0')
result = str_pad(self._parent, width, side='left', fillchar='0')
return self._wrap_result(result)

@copy(str_slice)
def slice(self, start=None, stop=None, step=None):
result = str_slice(self._data, start, stop, step)
result = str_slice(self._parent, start, stop, step)
return self._wrap_result(result)

@copy(str_slice_replace)
def slice_replace(self, start=None, stop=None, repl=None):
result = str_slice_replace(self._data, start, stop, repl)
result = str_slice_replace(self._parent, start, stop, repl)
return self._wrap_result(result)

@copy(str_decode)
def decode(self, encoding, errors="strict"):
result = str_decode(self._data, encoding, errors)
result = str_decode(self._parent, encoding, errors)
return self._wrap_result(result)

@copy(str_encode)
def encode(self, encoding, errors="strict"):
result = str_encode(self._data, encoding, errors)
result = str_encode(self._parent, encoding, errors)
return self._wrap_result(result)

_shared_docs['str_strip'] = (r"""
Expand Down Expand Up @@ -2663,38 +2663,38 @@ def encode(self, encoding, errors="strict"):
@Appender(_shared_docs['str_strip'] % dict(side='left and right sides',
method='strip'))
def strip(self, to_strip=None):
result = str_strip(self._data, to_strip, side='both')
result = str_strip(self._parent, to_strip, side='both')
return self._wrap_result(result)

@Appender(_shared_docs['str_strip'] % dict(side='left side',
method='lstrip'))
def lstrip(self, to_strip=None):
result = str_strip(self._data, to_strip, side='left')
result = str_strip(self._parent, to_strip, side='left')
return self._wrap_result(result)

@Appender(_shared_docs['str_strip'] % dict(side='right side',
method='rstrip'))
def rstrip(self, to_strip=None):
result = str_strip(self._data, to_strip, side='right')
result = str_strip(self._parent, to_strip, side='right')
return self._wrap_result(result)

@copy(str_wrap)
def wrap(self, width, **kwargs):
result = str_wrap(self._data, width, **kwargs)
result = str_wrap(self._parent, width, **kwargs)
return self._wrap_result(result)

@copy(str_get_dummies)
def get_dummies(self, sep='|'):
# we need to cast to Series of strings as only that has all
# methods available for making the dummies...
data = self._orig.astype(str) if self._is_categorical else self._data
data = self._orig.astype(str) if self._is_categorical else self._parent
result, name = str_get_dummies(data, sep)
return self._wrap_result(result, use_codes=(not self._is_categorical),
name=name, expand=True)

@copy(str_translate)
def translate(self, table, deletechars=None):
result = str_translate(self._data, table, deletechars)
result = str_translate(self._parent, table, deletechars)
return self._wrap_result(result)

count = _pat_wrapper(str_count, flags=True)
Expand Down Expand Up @@ -2737,14 +2737,15 @@ def extractall(self, pat, flags=0):
dict(side='lowest', method='find',
also='rfind : Return highest indexes in each strings'))
def find(self, sub, start=0, end=None):
result = str_find(self._data, sub, start=start, end=end, side='left')
result = str_find(self._parent, sub, start=start, end=end, side='left')
return self._wrap_result(result)

@Appender(_shared_docs['find'] %
dict(side='highest', method='rfind',
also='find : Return lowest indexes in each strings'))
def rfind(self, sub, start=0, end=None):
result = str_find(self._data, sub, start=start, end=end, side='right')
result = str_find(self._parent, sub,
start=start, end=end, side='right')
return self._wrap_result(result)

def normalize(self, form):
Expand All @@ -2763,7 +2764,7 @@ def normalize(self, form):
"""
import unicodedata
f = lambda x: unicodedata.normalize(form, compat.u_safe(x))
result = _na_map(f, self._data)
result = _na_map(f, self._parent)
return self._wrap_result(result)

_shared_docs['index'] = ("""
Expand Down Expand Up @@ -2794,14 +2795,16 @@ def normalize(self, form):
dict(side='lowest', similar='find', method='index',
also='rindex : Return highest indexes in each strings'))
def index(self, sub, start=0, end=None):
result = str_index(self._data, sub, start=start, end=end, side='left')
result = str_index(self._parent, sub,
start=start, end=end, side='left')
return self._wrap_result(result)

@Appender(_shared_docs['index'] %
dict(side='highest', similar='rfind', method='rindex',
also='index : Return lowest indexes in each strings'))
def rindex(self, sub, start=0, end=None):
result = str_index(self._data, sub, start=start, end=end, side='right')
result = str_index(self._parent, sub,
start=start, end=end, side='right')
return self._wrap_result(result)

_shared_docs['len'] = ("""
Expand Down
6 changes: 3 additions & 3 deletions pandas/plotting/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2727,7 +2727,7 @@ def _grouped_plot_by_column(plotf, data, columns=None, by=None,
class BasePlotMethods(PandasObject):

def __init__(self, data):
self._data = data
self._parent = data # can be Series or DataFrame

def __call__(self, *args, **kwargs):
raise NotImplementedError
Expand Down Expand Up @@ -2755,7 +2755,7 @@ def __call__(self, kind='line', ax=None,
rot=None, fontsize=None, colormap=None, table=False,
yerr=None, xerr=None,
label=None, secondary_y=False, **kwds):
return plot_series(self._data, kind=kind, ax=ax, figsize=figsize,
return plot_series(self._parent, kind=kind, ax=ax, figsize=figsize,
use_index=use_index, title=title, grid=grid,
legend=legend, style=style, logx=logx, logy=logy,
loglog=loglog, xticks=xticks, yticks=yticks,
Expand Down Expand Up @@ -2954,7 +2954,7 @@ def __call__(self, x=None, y=None, kind='line', ax=None,
rot=None, fontsize=None, colormap=None, table=False,
yerr=None, xerr=None,
secondary_y=False, sort_columns=False, **kwds):
return plot_frame(self._data, kind=kind, x=x, y=y, ax=ax,
return plot_frame(self._parent, kind=kind, x=x, y=y, ax=ax,
subplots=subplots, sharex=sharex, sharey=sharey,
layout=layout, figsize=figsize, use_index=use_index,
title=title, grid=grid, legend=legend, style=style,
Expand Down