Skip to content

Commit

Permalink
Replace deprecated call to StringMethods
Browse files Browse the repository at this point in the history
In Pandas 2, pd.Series.str is the right way and still works with Pandas
1
  • Loading branch information
caneff committed Sep 15, 2023
1 parent 4f5e2c5 commit 9358eb5
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 @@ -4907,9 +4907,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 @@ -4949,8 +4949,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 @@ -4987,8 +4987,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 @@ -5070,9 +5070,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 @@ -5081,9 +5081,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 @@ -5161,25 +5161,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 @@ -2936,7 +2936,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 9358eb5

Please sign in to comment.