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

ser.xs and df.xs issues #28507

Closed
wants to merge 1 commit into from
Closed
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
43 changes: 37 additions & 6 deletions sdks/python/apache_beam/dataframe/frames_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ def test_series_xs(self):
'num_wings': [0, 0, 2, 2],
'class': ['mammal', 'mammal', 'mammal', 'bird'],
'animal': ['cat', 'dog', 'bat', 'penguin'],
'locomotion': ['walks', 'walks', 'flies', 'walks']
'locomotion': ['walks', 'walks', 'flies', 'walks'],
}
df = pd.DataFrame(data=d)
df = df.set_index(['class', 'animal', 'locomotion'])
Expand All @@ -319,21 +319,52 @@ def test_series_xs(self):
lambda df: df.num_legs.xs(('bird', 'walks'), level=[0, 'locomotion']),
df)

df2 = df.assign(color=['brown', 'brown', 'black', 'black']).set_index(
'color', append=True)
# Ok with length 3 lookup if index is 4 dims.
self._run_test(lambda df: df.num_legs.xs(('mammal', 'dog', 'walks')), df2)

# Fails with lookup length == index number of dims
self._run_test(
lambda df: df.num_legs.xs(('mammal', 'dog')),
df.reset_index().set_index(['class', 'animal']))

# Fails with lookup length == index number of dims
self._run_test(lambda df: df.num_legs.xs(('mammal', 'dog', 'walks')), df)

# Fails with lookup length == index number of dims
self._run_test(
lambda df: df.num_legs.xs(('mammal', 'dog', 'walks', 'brown')), df2)

def test_dataframe_xs(self):
# Test cases reported in BEAM-13421
df = pd.DataFrame(
np.array([
['state', 'day1', 12],
['state', 'day1', 1],
['state', 'day2', 14],
['county', 'day1', 9],
['state', 'day1', 'A', 12],
['state', 'day1', 'B', 1],
['state', 'day2', 'A', 14],
['county', 'day1', 'B', 9],
]),
columns=['provider', 'time', 'value'])
columns=['provider', 'time', 'group', 'value'])

self._run_test(lambda df: df.xs('state'), df.set_index(['provider']))
self._run_test(
lambda df: df.xs('state'), df.set_index(['provider', 'time']))

# Ok with lookup length == index number of dims
self._run_test(
lambda df: df.xs(('state', 'day1')), df.set_index(['provider', 'time']))

# Ok with lookup length < index number of dims
self._run_test(
lambda df: df.xs(('state', 'day1')),
df.set_index(['provider', 'time', 'group']))

# Fails, shape isn't correct with lookup length == index number of dims = 3
self._run_test(
lambda df: df.xs(('state', 'day1', 'A')),
df.set_index(['provider', 'time', 'group']))

def test_set_column(self):
def new_column(df):
df['NewCol'] = df['Speed']
Expand Down
Loading