|
3 | 3 | import numpy as np
|
4 | 4 | import pytest
|
5 | 5 |
|
| 6 | +import pandas.util._test_decorators as td |
| 7 | + |
6 | 8 | import pandas as pd
|
7 | 9 | from pandas import (
|
8 | 10 | Categorical,
|
|
14 | 16 | Series,
|
15 | 17 | Timedelta,
|
16 | 18 | Timestamp,
|
| 19 | + option_context, |
17 | 20 | )
|
18 | 21 | import pandas._testing as tm
|
19 | 22 |
|
@@ -428,38 +431,42 @@ def test_ix_categorical_index(self):
|
428 | 431 | expect = DataFrame(df.loc[:, ["X", "Y"]], index=cdf.index, columns=exp_columns)
|
429 | 432 | tm.assert_frame_equal(cdf.loc[:, ["X", "Y"]], expect)
|
430 | 433 |
|
431 |
| - def test_ix_categorical_index_non_unique(self): |
| 434 | + @pytest.mark.parametrize( |
| 435 | + "infer_string", [False, pytest.param(True, marks=td.skip_if_no("pyarrow"))] |
| 436 | + ) |
| 437 | + def test_ix_categorical_index_non_unique(self, infer_string): |
432 | 438 | # non-unique
|
433 |
| - df = DataFrame( |
434 |
| - np.random.default_rng(2).standard_normal((3, 3)), |
435 |
| - index=list("ABA"), |
436 |
| - columns=list("XYX"), |
437 |
| - ) |
438 |
| - cdf = df.copy() |
439 |
| - cdf.index = CategoricalIndex(df.index) |
440 |
| - cdf.columns = CategoricalIndex(df.columns) |
441 |
| - |
442 |
| - exp_index = CategoricalIndex(list("AA"), categories=["A", "B"]) |
443 |
| - expect = DataFrame(df.loc["A", :], columns=cdf.columns, index=exp_index) |
444 |
| - tm.assert_frame_equal(cdf.loc["A", :], expect) |
445 |
| - |
446 |
| - exp_columns = CategoricalIndex(list("XX"), categories=["X", "Y"]) |
447 |
| - expect = DataFrame(df.loc[:, "X"], index=cdf.index, columns=exp_columns) |
448 |
| - tm.assert_frame_equal(cdf.loc[:, "X"], expect) |
449 |
| - |
450 |
| - expect = DataFrame( |
451 |
| - df.loc[["A", "B"], :], |
452 |
| - columns=cdf.columns, |
453 |
| - index=CategoricalIndex(list("AAB")), |
454 |
| - ) |
455 |
| - tm.assert_frame_equal(cdf.loc[["A", "B"], :], expect) |
456 |
| - |
457 |
| - expect = DataFrame( |
458 |
| - df.loc[:, ["X", "Y"]], |
459 |
| - index=cdf.index, |
460 |
| - columns=CategoricalIndex(list("XXY")), |
461 |
| - ) |
462 |
| - tm.assert_frame_equal(cdf.loc[:, ["X", "Y"]], expect) |
| 439 | + with option_context("future.infer_string", infer_string): |
| 440 | + df = DataFrame( |
| 441 | + np.random.default_rng(2).standard_normal((3, 3)), |
| 442 | + index=list("ABA"), |
| 443 | + columns=list("XYX"), |
| 444 | + ) |
| 445 | + cdf = df.copy() |
| 446 | + cdf.index = CategoricalIndex(df.index) |
| 447 | + cdf.columns = CategoricalIndex(df.columns) |
| 448 | + |
| 449 | + exp_index = CategoricalIndex(list("AA"), categories=["A", "B"]) |
| 450 | + expect = DataFrame(df.loc["A", :], columns=cdf.columns, index=exp_index) |
| 451 | + tm.assert_frame_equal(cdf.loc["A", :], expect) |
| 452 | + |
| 453 | + exp_columns = CategoricalIndex(list("XX"), categories=["X", "Y"]) |
| 454 | + expect = DataFrame(df.loc[:, "X"], index=cdf.index, columns=exp_columns) |
| 455 | + tm.assert_frame_equal(cdf.loc[:, "X"], expect) |
| 456 | + |
| 457 | + expect = DataFrame( |
| 458 | + df.loc[["A", "B"], :], |
| 459 | + columns=cdf.columns, |
| 460 | + index=CategoricalIndex(list("AAB")), |
| 461 | + ) |
| 462 | + tm.assert_frame_equal(cdf.loc[["A", "B"], :], expect) |
| 463 | + |
| 464 | + expect = DataFrame( |
| 465 | + df.loc[:, ["X", "Y"]], |
| 466 | + index=cdf.index, |
| 467 | + columns=CategoricalIndex(list("XXY")), |
| 468 | + ) |
| 469 | + tm.assert_frame_equal(cdf.loc[:, ["X", "Y"]], expect) |
463 | 470 |
|
464 | 471 | def test_loc_slice(self, df):
|
465 | 472 | # GH9748
|
|
0 commit comments