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 handling of copy=None defaults for Pandas 2 #28523

Merged
merged 2 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 9 additions & 1 deletion sdks/python/apache_beam/dataframe/frame_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,15 @@ def wrap(func):
def wrapper(**kwargs):
for name in defaults_to_populate:
if name not in kwargs:
kwargs[name] = arg_to_default[name]
# In pandas 2, many methods rely on the default copy=None
# to mean that copy is the value of copy_on_write. Since
# copy_on_write will always be true for Beam, just fill it
# in here. In pandas 1, the default was True anyway.
if name == 'copy' and arg_to_default[name] is None:
caneff marked this conversation as resolved.
Show resolved Hide resolved
kwargs[name] = True
else:
kwargs[name] = arg_to_default[name]

return func(**kwargs)

return wrapper
Expand Down
15 changes: 15 additions & 0 deletions sdks/python/apache_beam/dataframe/frame_base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,21 @@ def func(self, a, **kwargs):
'a': 2, 'b': 4, 'c': 6, 'kw_only': 8
})

def test_populate_defaults_overwrites_copy(self):
class Base(object):
def func(self, a=1, b=2, c=3, *, copy=None):
pass

class Proxy(object):
@frame_base.args_to_kwargs(Base)
@frame_base.populate_defaults(Base)
def func(self, a, copy, **kwargs):
return dict(kwargs, a=a, copy=copy)

proxy = Proxy()
self.assertEqual(proxy.func(), {'a': 1, 'copy': True})
self.assertEqual(proxy.func(copy=False), {'a': 1, 'copy': False})


if __name__ == '__main__':
unittest.main()
Loading