Skip to content

Commit 1beecf1

Browse files
CLN: Refactor code and clean up according to PR feedback
1 parent 63caeb6 commit 1beecf1

File tree

2 files changed

+28
-30
lines changed

2 files changed

+28
-30
lines changed

Diff for: pandas/tests/reshape/test_pivot.py

+9-14
Original file line numberDiff line numberDiff line change
@@ -2856,26 +2856,21 @@ def test_pivot_margins_with_none_index(self):
28562856
@pytest.mark.filterwarnings("ignore:Passing a BlockManager:DeprecationWarning")
28572857
def test_pivot_with_pyarrow_categorical(self):
28582858
# GH#53051
2859-
28602859
pa = pytest.importorskip("pyarrow")
28612860

2862-
# Create dataframe with categorical column
2863-
df = DataFrame(
2864-
{"string_column": ["A", "B", "C"], "number_column": [1, 2, 3]}
2865-
).astype({"string_column": "category", "number_column": "float32"})
2866-
2867-
# Convert dataframe to pyarrow backend
2868-
df = df.astype(
2869-
{
2870-
"string_column": ArrowDtype(pa.dictionary(pa.int32(), pa.string())),
2871-
"number_column": "float[pyarrow]",
2872-
}
2861+
df = (
2862+
DataFrame({"string_column": ["A", "B", "C"], "number_column": [1, 2, 3]})
2863+
.astype({"string_column": "category", "number_column": "float32"})
2864+
.astype(
2865+
{
2866+
"string_column": ArrowDtype(pa.dictionary(pa.int32(), pa.string())),
2867+
"number_column": "float[pyarrow]",
2868+
}
2869+
)
28732870
)
28742871

2875-
# Check that pivot works
28762872
df = df.pivot(columns=["string_column"], values=["number_column"])
28772873

2878-
# Assert that values of result are correct to prevent silent failure
28792874
multi_index = MultiIndex.from_arrays(
28802875
[["number_column", "number_column", "number_column"], ["A", "B", "C"]],
28812876
names=(None, "string_column"),

Diff for: pandas/tests/test_multilevel.py

+19-16
Original file line numberDiff line numberDiff line change
@@ -319,29 +319,32 @@ def test_multiindex_dt_with_nan(self):
319319
expected = Series(["a", "b", "c", "d"], name=("sub", np.nan))
320320
tm.assert_series_equal(result, expected)
321321

322-
# Ignore deprecation raised by old versions of pyarrow. Already fixed in
323-
# newer versions
324322
@pytest.mark.filterwarnings("ignore:Passing a BlockManager:DeprecationWarning")
325323
def test_multiindex_with_pyarrow_categorical(self):
326324
# GH#53051
327-
328325
pa = pytest.importorskip("pyarrow")
329326

330-
# Create dataframe with categorical column
331-
df = DataFrame(
332-
{"string_column": ["A", "B", "C"], "number_column": [1, 2, 3]}
333-
).astype({"string_column": "category", "number_column": "float32"})
334-
335-
# Convert dataframe to pyarrow backend
336-
df = df.astype(
337-
{
338-
"string_column": ArrowDtype(pa.dictionary(pa.int32(), pa.string())),
339-
"number_column": "float[pyarrow]",
340-
}
327+
df = (
328+
DataFrame({"string_column": ["A", "B", "C"], "number_column": [1, 2, 3]})
329+
.astype({"string_column": "category", "number_column": "float32"})
330+
.astype(
331+
{
332+
"string_column": ArrowDtype(pa.dictionary(pa.int32(), pa.string())),
333+
"number_column": "float[pyarrow]",
334+
}
335+
)
341336
)
342337

343-
# Check that index can be set
344-
df.set_index(["string_column", "number_column"])
338+
df = df.set_index(["string_column", "number_column"])
339+
340+
df_expected = DataFrame(
341+
index=MultiIndex.from_arrays(
342+
[["A", "B", "C"], [1, 2, 3]], names=["string_column", "number_column"]
343+
)
344+
)
345+
tm.assert_frame_equal(
346+
df, df_expected, check_dtype=False, check_column_type=False
347+
)
345348

346349

347350
class TestSorted:

0 commit comments

Comments
 (0)