Skip to content
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

Change pd.core.strings.StringMethods for Pandas 2 compatability. #28455

Merged
merged 3 commits into from
Sep 20, 2023
Merged
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
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)
caneff marked this conversation as resolved.
Show resolved Hide resolved
@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
Loading