Skip to content

Standardize accessor naming convention self._parent, avoid self._data #19309

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

Closed
wants to merge 5 commits into from
Closed
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
1 change: 1 addition & 0 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -2163,6 +2163,7 @@ class CategoricalAccessor(PandasDelegate, PandasObject, NoNewAttributesMixin):

def __init__(self, data):
self._validate(data)
self._parent = data
self.categorical = data.values
self.index = data.index
self.name = data.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 @@ -28,14 +28,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
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
71 changes: 37 additions & 34 deletions pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ def str_extract(arr, pat, flags=0, expand=None):
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 @@ -1312,7 +1312,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 @@ -1326,15 +1326,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 @@ -1372,7 +1372,7 @@ class StringMethods(NoNewAttributesMixin):
def __init__(self, data):
self._validate(data)
self._is_categorical = is_categorical_dtype(data)
self._data = data.cat.categories if self._is_categorical else data
self._parent = data.cat.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 @@ -1502,18 +1502,18 @@ def cons_row(x):

@copy(str_cat)
def cat(self, others=None, sep=None, na_rep=None):
data = self._orig if self._is_categorical else self._data
data = self._orig if self._is_categorical else self._parent
result = str_cat(data, others=others, sep=sep, na_rep=na_rep)
return self._wrap_result(result, use_codes=(not self._is_categorical))

@copy(str_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)

@copy(str_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 @@ -1568,7 +1568,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 @@ -1579,45 +1579,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):
result = str_replace(self._data, pat, repl, n=n, case=case,
result = str_replace(self._parent, pat, repl, n=n, case=case,
flags=flags)
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 @@ -1665,27 +1665,27 @@ def zfill(self, width):
-------
filled : Series/Index of objects
"""
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'] = ("""
Expand All @@ -1700,38 +1700,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 @@ -1774,14 +1774,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 @@ -1800,7 +1801,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 @@ -1831,14 +1832,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 @@ -2470,7 +2470,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

def __call__(self, *args, **kwargs):
raise NotImplementedError
Expand Down Expand Up @@ -2498,7 +2498,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 @@ -2655,7 +2655,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