Skip to content

Commit

Permalink
Change pd.core.strings.StringMethods for Pandas 2 compatability. (#28455
Browse files Browse the repository at this point in the history
)
  • Loading branch information
caneff authored Sep 20, 2023
1 parent a7335cb commit 9327448
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
34 changes: 17 additions & 17 deletions sdks/python/apache_beam/dataframe/frames.py
Original file line number Diff line number Diff line change
Expand Up @@ -4931,9 +4931,9 @@ def __setitem__(self, index, value):


class _DeferredStringMethods(frame_base.DeferredBase):
@frame_base.with_docs_from(pd.core.strings.StringMethods)
@frame_base.args_to_kwargs(pd.core.strings.StringMethods)
@frame_base.populate_defaults(pd.core.strings.StringMethods)
@frame_base.with_docs_from(pd.Series.str)
@frame_base.args_to_kwargs(pd.Series.str)
@frame_base.populate_defaults(pd.Series.str)
def cat(self, others, join, **kwargs):
"""If defined, ``others`` must be a :class:`DeferredSeries` or a ``list`` of
``DeferredSeries``."""
Expand Down Expand Up @@ -4973,8 +4973,8 @@ def func(*args):
requires_partition_by=requires,
preserves_partition_by=partitionings.Arbitrary()))

@frame_base.with_docs_from(pd.core.strings.StringMethods)
@frame_base.args_to_kwargs(pd.core.strings.StringMethods)
@frame_base.with_docs_from(pd.Series.str)
@frame_base.args_to_kwargs(pd.Series.str)
def repeat(self, repeats):
"""``repeats`` must be an ``int`` or a :class:`DeferredSeries`. Lists are
not supported because they make this operation order-sensitive."""
Expand Down Expand Up @@ -5011,8 +5011,8 @@ def repeat(self, repeats):
raise TypeError("str.repeat(repeats=) value must be an int or a "
f"DeferredSeries (encountered {type(repeats)}).")

@frame_base.with_docs_from(pd.core.strings.StringMethods)
@frame_base.args_to_kwargs(pd.core.strings.StringMethods)
@frame_base.with_docs_from(pd.Series.str)
@frame_base.args_to_kwargs(pd.Series.str)
def get_dummies(self, **kwargs):
"""
Series must be categorical dtype. Please cast to ``CategoricalDtype``
Expand Down Expand Up @@ -5094,9 +5094,9 @@ def func(s):
requires_partition_by=partitionings.Arbitrary(),
preserves_partition_by=partitionings.Arbitrary()))

@frame_base.with_docs_from(pd.core.strings.StringMethods)
@frame_base.args_to_kwargs(pd.core.strings.StringMethods)
@frame_base.populate_defaults(pd.core.strings.StringMethods)
@frame_base.with_docs_from(pd.Series.str)
@frame_base.args_to_kwargs(pd.Series.str)
@frame_base.populate_defaults(pd.Series.str)
def split(self, **kwargs):
"""
Like other non-deferred methods, dtype must be CategoricalDtype.
Expand All @@ -5105,9 +5105,9 @@ def split(self, **kwargs):
"""
return self._split_helper(rsplit=False, **kwargs)

@frame_base.with_docs_from(pd.core.strings.StringMethods)
@frame_base.args_to_kwargs(pd.core.strings.StringMethods)
@frame_base.populate_defaults(pd.core.strings.StringMethods)
@frame_base.with_docs_from(pd.Series.str)
@frame_base.args_to_kwargs(pd.Series.str)
@frame_base.populate_defaults(pd.Series.str)
def rsplit(self, **kwargs):
"""
Like other non-deferred methods, dtype must be CategoricalDtype.
Expand Down Expand Up @@ -5185,25 +5185,25 @@ def func(df, *args, **kwargs):
return func

for method in ELEMENTWISE_STRING_METHODS:
if not hasattr(pd.core.strings.StringMethods, method):
if not hasattr(pd.Series.str, method):
# older versions (1.0.x) don't support some of these methods
continue
setattr(_DeferredStringMethods,
method,
frame_base._elementwise_method(make_str_func(method),
name=method,
base=pd.core.strings.StringMethods))
base=pd.Series.str))

for method in NON_ELEMENTWISE_STRING_METHODS:
if not hasattr(pd.core.strings.StringMethods, method):
if not hasattr(pd.Series.str, method):
# older versions (1.0.x) don't support some of these methods
continue
setattr(_DeferredStringMethods,
method,
frame_base._proxy_method(
make_str_func(method),
name=method,
base=pd.core.strings.StringMethods,
base=pd.Series.str,
requires_partition_by=partitionings.Arbitrary(),
preserves_partition_by=partitionings.Singleton()))

Expand Down
2 changes: 1 addition & 1 deletion sdks/python/apache_beam/dataframe/frames_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2986,7 +2986,7 @@ class DocstringTest(unittest.TestCase):
(frames.DeferredDataFrame, pd.DataFrame),
(frames.DeferredSeries, pd.Series),
#(frames._DeferredIndex, pd.Index),
(frames._DeferredStringMethods, pd.core.strings.StringMethods),
(frames._DeferredStringMethods, pd.Series.str),
(
frames._DeferredCategoricalMethods,
pd.core.arrays.categorical.CategoricalAccessor),
Expand Down

0 comments on commit 9327448

Please sign in to comment.