yeandy commented on a change in pull request #17026:
URL: https://github.com/apache/beam/pull/17026#discussion_r837530657
##########
File path: sdks/python/apache_beam/dataframe/frames_test.py
##########
@@ -1295,6 +1310,118 @@ def s_times_shuffled(times, s):
self._run_test(lambda s: s.pipe(s_times, 2), s)
self._run_test(lambda s: s.pipe((s_times_shuffled, 's'), 2), s)
+ def test_unstack_pandas_series_not_multiindex(self):
+ # Pandas should throw a ValueError if performing unstack
+ # on a Series without MultiIndex
+ s = pd.Series([1, 2, 3, 4], index=['one', 'two', 'three', 'four'])
+ with self.assertRaises((AttributeError, ValueError)):
+ self._run_test(lambda s: s.unstack(), s)
+
+ def test_unstack_non_categorical_index(self):
+ index = pd.MultiIndex.from_tuples([('one', 'a'), ('one', 'b'), ('two',
'a'),
+ ('two', 'b')])
+ index = index.set_levels(
+ index.levels[0].astype(pd.CategoricalDtype(['one', 'two'])), level=0)
+ s = pd.Series(np.arange(1.0, 5.0), index=index)
+ with self.assertRaisesRegex(
+ frame_base.WontImplementError,
+ r"unstack\(\) is only supported on DataFrames if"):
+ self._run_test(lambda s: s.unstack(level=-1), s)
+
+ def _unstack_get_categorical_index(self):
+ index = pd.MultiIndex.from_tuples([('one', 'a'), ('one', 'b'), ('two',
'a'),
+ ('two', 'b')])
+ index = index.set_levels(
+ index.levels[0].astype(pd.CategoricalDtype(['one', 'two'])), level=0)
+ index = index.set_levels(
+ index.levels[1].astype(pd.CategoricalDtype(['a', 'b'])), level=1)
+ return index
+
+ def test_unstack_pandas_example1(self):
+ index = self._unstack_get_categorical_index()
+ s = pd.Series(np.arange(1.0, 5.0), index=index)
+ self._run_test(lambda s: s.unstack(level=-1), s, check_dtypes=False)
+
+ def test_unstack_pandas_example2(self):
+ index = self._unstack_get_categorical_index()
+ s = pd.Series(np.arange(1.0, 5.0), index=index)
+ self._run_test(lambda s: s.unstack(level=0), s, check_dtypes=False)
+
+ def test_unstack_pandas_example3(self):
+ index = self._unstack_get_categorical_index()
+ s = pd.Series(np.arange(1.0, 5.0), index=index)
+ df = s.unstack(level=0)
+ if PD_VERSION < (1, 2):
+ with self.assertRaisesRegex(frame_base.WontImplementError,
+ r"pandas==1.1.5 has an indexing error bug"):
+ self._run_test(lambda df: df.unstack(), df, check_dtypes=False)
+ else:
+ self._run_test(lambda df: df.unstack(), df, check_dtypes=False)
+
+ @unittest.skipIf(
+ PD_VERSION < (1, 4),
+ "pandas=1.4 fixes error when concat() of boolean types results in
object")
Review comment:
In the test definition, we try to set the type of one of the index
levels to boolean.
`index = index.set_levels(index.levels[0].astype('boolean'), level=0)`
But for pandas < 1.4, trying to set the index to boolean returns `object`
for dtype instead of `boolean`.
```
>>> index.levels[0].astype('boolean')
Index([False, True], dtype='object')
```
I can fix the message.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]