Skip to content

Commit d52748c

Browse files
jbrockmendeljreback
authored andcommitted
Change ._data to ._parent for accessors (#21906)
1 parent e5a2969 commit d52748c

File tree

4 files changed

+46
-43
lines changed

4 files changed

+46
-43
lines changed

pandas/core/arrays/categorical.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -2401,7 +2401,7 @@ class CategoricalAccessor(PandasDelegate, PandasObject, NoNewAttributesMixin):
24012401

24022402
def __init__(self, data):
24032403
self._validate(data)
2404-
self.categorical = data.values
2404+
self._parent = data.values
24052405
self.index = data.index
24062406
self.name = data.name
24072407
self._freeze()
@@ -2413,19 +2413,19 @@ def _validate(data):
24132413
"'category' dtype")
24142414

24152415
def _delegate_property_get(self, name):
2416-
return getattr(self.categorical, name)
2416+
return getattr(self._parent, name)
24172417

24182418
def _delegate_property_set(self, name, new_values):
2419-
return setattr(self.categorical, name, new_values)
2419+
return setattr(self._parent, name, new_values)
24202420

24212421
@property
24222422
def codes(self):
24232423
from pandas import Series
2424-
return Series(self.categorical.codes, index=self.index)
2424+
return Series(self._parent.codes, index=self.index)
24252425

24262426
def _delegate_method(self, name, *args, **kwargs):
24272427
from pandas import Series
2428-
method = getattr(self.categorical, name)
2428+
method = getattr(self._parent, name)
24292429
res = method(*args, **kwargs)
24302430
if res is not None:
24312431
return Series(res, index=self.index, name=self.name)

pandas/core/indexes/accessors.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ def __init__(self, data, orig):
2727
raise TypeError("cannot convert an object of type {0} to a "
2828
"datetimelike index".format(type(data)))
2929

30-
self.values = data
30+
self._parent = data
3131
self.orig = orig
3232
self.name = getattr(data, 'name', None)
3333
self.index = getattr(data, 'index', None)
3434
self._freeze()
3535

3636
def _get_values(self):
37-
data = self.values
37+
data = self._parent
3838
if is_datetime64_dtype(data.dtype):
3939
return DatetimeIndex(data, copy=False, name=self.name)
4040

pandas/core/strings.py

+36-33
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ def str_extract(arr, pat, flags=0, expand=True):
927927
if expand:
928928
return _str_extract_frame(arr._orig, pat, flags=flags)
929929
else:
930-
result, name = _str_extract_noexpand(arr._data, pat, flags=flags)
930+
result, name = _str_extract_noexpand(arr._parent, pat, flags=flags)
931931
return arr._wrap_result(result, name=name, expand=expand)
932932

933933

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

17221722
def _noarg_wrapper(f, docstring=None, **kargs):
17231723
def wrapper(self):
1724-
result = _na_map(f, self._data, **kargs)
1724+
result = _na_map(f, self._parent, **kargs)
17251725
return self._wrap_result(result)
17261726

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

17361736
def _pat_wrapper(f, flags=False, na=False, **kwargs):
17371737
def wrapper1(self, pat):
1738-
result = f(self._data, pat)
1738+
result = f(self._parent, pat)
17391739
return self._wrap_result(result)
17401740

17411741
def wrapper2(self, pat, flags=0, **kwargs):
1742-
result = f(self._data, pat, flags=flags, **kwargs)
1742+
result = f(self._parent, pat, flags=flags, **kwargs)
17431743
return self._wrap_result(result)
17441744

17451745
def wrapper3(self, pat, na=np.nan):
1746-
result = f(self._data, pat, na=na)
1746+
result = f(self._parent, pat, na=na)
17471747
return self._wrap_result(result)
17481748

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

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

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

23472347
_shared_docs['str_partition'] = ("""
@@ -2432,7 +2432,7 @@ def rsplit(self, pat=None, n=-1, expand=False):
24322432
})
24332433
def partition(self, pat=' ', expand=True):
24342434
f = lambda x: x.partition(pat)
2435-
result = _na_map(f, self._data)
2435+
result = _na_map(f, self._parent)
24362436
return self._wrap_result(result, expand=expand)
24372437

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

24492449
@copy(str_get)
24502450
def get(self, i):
2451-
result = str_get(self._data, i)
2451+
result = str_get(self._parent, i)
24522452
return self._wrap_result(result)
24532453

24542454
@copy(str_join)
24552455
def join(self, sep):
2456-
result = str_join(self._data, sep)
2456+
result = str_join(self._parent, sep)
24572457
return self._wrap_result(result)
24582458

24592459
@copy(str_contains)
24602460
def contains(self, pat, case=True, flags=0, na=np.nan, regex=True):
2461-
result = str_contains(self._data, pat, case=case, flags=flags, na=na,
2461+
result = str_contains(self._parent, pat, case=case, flags=flags, na=na,
24622462
regex=regex)
24632463
return self._wrap_result(result)
24642464

24652465
@copy(str_match)
24662466
def match(self, pat, case=True, flags=0, na=np.nan, as_indexer=None):
2467-
result = str_match(self._data, pat, case=case, flags=flags, na=na,
2467+
result = str_match(self._parent, pat, case=case, flags=flags, na=na,
24682468
as_indexer=as_indexer)
24692469
return self._wrap_result(result)
24702470

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

24772477
@copy(str_repeat)
24782478
def repeat(self, repeats):
2479-
result = str_repeat(self._data, repeats)
2479+
result = str_repeat(self._parent, repeats)
24802480
return self._wrap_result(result)
24812481

24822482
@copy(str_pad)
24832483
def pad(self, width, side='left', fillchar=' '):
2484-
result = str_pad(self._data, width, side=side, fillchar=fillchar)
2484+
result = str_pad(self._parent, width, side=side, fillchar=fillchar)
24852485
return self._wrap_result(result)
24862486

24872487
_shared_docs['str_pad'] = ("""
@@ -2574,27 +2574,27 @@ def zfill(self, width):
25742574
4 NaN
25752575
dtype: object
25762576
"""
2577-
result = str_pad(self._data, width, side='left', fillchar='0')
2577+
result = str_pad(self._parent, width, side='left', fillchar='0')
25782578
return self._wrap_result(result)
25792579

25802580
@copy(str_slice)
25812581
def slice(self, start=None, stop=None, step=None):
2582-
result = str_slice(self._data, start, stop, step)
2582+
result = str_slice(self._parent, start, stop, step)
25832583
return self._wrap_result(result)
25842584

25852585
@copy(str_slice_replace)
25862586
def slice_replace(self, start=None, stop=None, repl=None):
2587-
result = str_slice_replace(self._data, start, stop, repl)
2587+
result = str_slice_replace(self._parent, start, stop, repl)
25882588
return self._wrap_result(result)
25892589

25902590
@copy(str_decode)
25912591
def decode(self, encoding, errors="strict"):
2592-
result = str_decode(self._data, encoding, errors)
2592+
result = str_decode(self._parent, encoding, errors)
25932593
return self._wrap_result(result)
25942594

25952595
@copy(str_encode)
25962596
def encode(self, encoding, errors="strict"):
2597-
result = str_encode(self._data, encoding, errors)
2597+
result = str_encode(self._parent, encoding, errors)
25982598
return self._wrap_result(result)
25992599

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

26692669
@Appender(_shared_docs['str_strip'] % dict(side='left side',
26702670
method='lstrip'))
26712671
def lstrip(self, to_strip=None):
2672-
result = str_strip(self._data, to_strip, side='left')
2672+
result = str_strip(self._parent, to_strip, side='left')
26732673
return self._wrap_result(result)
26742674

26752675
@Appender(_shared_docs['str_strip'] % dict(side='right side',
26762676
method='rstrip'))
26772677
def rstrip(self, to_strip=None):
2678-
result = str_strip(self._data, to_strip, side='right')
2678+
result = str_strip(self._parent, to_strip, side='right')
26792679
return self._wrap_result(result)
26802680

26812681
@copy(str_wrap)
26822682
def wrap(self, width, **kwargs):
2683-
result = str_wrap(self._data, width, **kwargs)
2683+
result = str_wrap(self._parent, width, **kwargs)
26842684
return self._wrap_result(result)
26852685

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

26952695
@copy(str_translate)
26962696
def translate(self, table, deletechars=None):
2697-
result = str_translate(self._data, table, deletechars)
2697+
result = str_translate(self._parent, table, deletechars)
26982698
return self._wrap_result(result)
26992699

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

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

27502751
def normalize(self, form):
@@ -2763,7 +2764,7 @@ def normalize(self, form):
27632764
"""
27642765
import unicodedata
27652766
f = lambda x: unicodedata.normalize(form, compat.u_safe(x))
2766-
result = _na_map(f, self._data)
2767+
result = _na_map(f, self._parent)
27672768
return self._wrap_result(result)
27682769

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

28002802
@Appender(_shared_docs['index'] %
28012803
dict(side='highest', similar='rfind', method='rindex',
28022804
also='index : Return lowest indexes in each strings'))
28032805
def rindex(self, sub, start=0, end=None):
2804-
result = str_index(self._data, sub, start=start, end=end, side='right')
2806+
result = str_index(self._parent, sub,
2807+
start=start, end=end, side='right')
28052808
return self._wrap_result(result)
28062809

28072810
_shared_docs['len'] = ("""

pandas/plotting/_core.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2727,7 +2727,7 @@ def _grouped_plot_by_column(plotf, data, columns=None, by=None,
27272727
class BasePlotMethods(PandasObject):
27282728

27292729
def __init__(self, data):
2730-
self._data = data
2730+
self._parent = data # can be Series or DataFrame
27312731

27322732
def __call__(self, *args, **kwargs):
27332733
raise NotImplementedError
@@ -2755,7 +2755,7 @@ def __call__(self, kind='line', ax=None,
27552755
rot=None, fontsize=None, colormap=None, table=False,
27562756
yerr=None, xerr=None,
27572757
label=None, secondary_y=False, **kwds):
2758-
return plot_series(self._data, kind=kind, ax=ax, figsize=figsize,
2758+
return plot_series(self._parent, kind=kind, ax=ax, figsize=figsize,
27592759
use_index=use_index, title=title, grid=grid,
27602760
legend=legend, style=style, logx=logx, logy=logy,
27612761
loglog=loglog, xticks=xticks, yticks=yticks,
@@ -2954,7 +2954,7 @@ def __call__(self, x=None, y=None, kind='line', ax=None,
29542954
rot=None, fontsize=None, colormap=None, table=False,
29552955
yerr=None, xerr=None,
29562956
secondary_y=False, sort_columns=False, **kwds):
2957-
return plot_frame(self._data, kind=kind, x=x, y=y, ax=ax,
2957+
return plot_frame(self._parent, kind=kind, x=x, y=y, ax=ax,
29582958
subplots=subplots, sharex=sharex, sharey=sharey,
29592959
layout=layout, figsize=figsize, use_index=use_index,
29602960
title=title, grid=grid, legend=legend, style=style,

0 commit comments

Comments
 (0)