MCPcopy
hub / github.com/pandas-dev/pandas / test_unstack

Method test_unstack

pandas/tests/extension/base/reshaping.py:305–342  ·  view source on GitHub ↗
(self, data, index, obj)

Source from the content-addressed store, hash-verified

303 )
304 @pytest.mark.parametrize("obj", ["series", "frame"])
305 def test_unstack(self, data, index, obj):
306 final_length = min(len(index), len(data))
307 index = index[:final_length]
308 data = data[:final_length]
309 if obj == "series":
310 ser = pd.Series(data, index=index)
311 else:
312 ser = pd.DataFrame({"A": data, "B": data}, index=index)
313
314 n = index.nlevels
315 levels = list(range(n))
316 # [0, 1, 2]
317 # [(0,), (1,), (2,), (0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)]
318 combinations = itertools.chain.from_iterable(
319 itertools.permutations(levels, i) for i in range(1, n)
320 )
321
322 for level in combinations:
323 result = ser.unstack(level=level)
324 assert all(
325 isinstance(result[col].array, type(data)) for col in result.columns
326 )
327
328 if obj == "series":
329 # We should get the same result with to_frame+unstack+droplevel
330 df = ser.to_frame()
331
332 alt = df.unstack(level=level).droplevel(0, axis=1)
333 tm.assert_frame_equal(result, alt)
334
335 obj_ser = ser.astype(object)
336
337 expected = obj_ser.unstack(level=level, fill_value=data.dtype.na_value)
338 if obj == "series":
339 assert (expected.dtypes == object).all()
340
341 result = result.astype(object)
342 tm.assert_frame_equal(result, expected)
343
344 def test_ravel(self, data):
345 # as long as EA is 1D-only, ravel is a no-op

Callers

nothing calls this directly

Calls 7

unstackMethod · 0.95
minFunction · 0.85
to_frameMethod · 0.45
droplevelMethod · 0.45
unstackMethod · 0.45
astypeMethod · 0.45
allMethod · 0.45

Tested by

no test coverage detected