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

TST: Use more explicit object names #55033

Merged
merged 4 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
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
24 changes: 17 additions & 7 deletions pandas/tests/frame/methods/test_reindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
isna,
)
import pandas._testing as tm
from pandas.api.types import CategoricalDtype as CDT
from pandas.api.types import CategoricalDtype


class TestReindexSetIndex:
Expand Down Expand Up @@ -1082,7 +1082,9 @@ def test_reindex_with_categoricalindex(self):
{
"A": np.arange(3, dtype="int64"),
},
index=CategoricalIndex(list("abc"), dtype=CDT(list("cabe")), name="B"),
index=CategoricalIndex(
list("abc"), dtype=CategoricalDtype(list("cabe")), name="B"
),
)

# reindexing
Expand Down Expand Up @@ -1111,13 +1113,13 @@ def test_reindex_with_categoricalindex(self):

result = df.reindex(Categorical(["a", "e"], categories=cats))
expected = DataFrame(
{"A": [0, np.nan], "B": Series(list("ae")).astype(CDT(cats))}
{"A": [0, np.nan], "B": Series(list("ae")).astype(CategoricalDtype(cats))}
).set_index("B")
tm.assert_frame_equal(result, expected, check_index_type=True)

result = df.reindex(Categorical(["a"], categories=cats))
expected = DataFrame(
{"A": [0], "B": Series(list("a")).astype(CDT(cats))}
{"A": [0], "B": Series(list("a")).astype(CategoricalDtype(cats))}
).set_index("B")
tm.assert_frame_equal(result, expected, check_index_type=True)

Expand All @@ -1138,21 +1140,29 @@ def test_reindex_with_categoricalindex(self):
# give back the type of categorical that we received
result = df.reindex(Categorical(["a", "e"], categories=cats, ordered=True))
expected = DataFrame(
{"A": [0, np.nan], "B": Series(list("ae")).astype(CDT(cats, ordered=True))}
{
"A": [0, np.nan],
"B": Series(list("ae")).astype(CategoricalDtype(cats, ordered=True)),
}
).set_index("B")
tm.assert_frame_equal(result, expected, check_index_type=True)

result = df.reindex(Categorical(["a", "d"], categories=["a", "d"]))
expected = DataFrame(
{"A": [0, np.nan], "B": Series(list("ad")).astype(CDT(["a", "d"]))}
{
"A": [0, np.nan],
"B": Series(list("ad")).astype(CategoricalDtype(["a", "d"])),
}
).set_index("B")
tm.assert_frame_equal(result, expected, check_index_type=True)

df2 = DataFrame(
{
"A": np.arange(6, dtype="int64"),
},
index=CategoricalIndex(list("aabbca"), dtype=CDT(list("cabe")), name="B"),
index=CategoricalIndex(
list("aabbca"), dtype=CategoricalDtype(list("cabe")), name="B"
),
)
# passed duplicate indexers are not allowed
msg = "cannot reindex on an axis with duplicate labels"
Expand Down
55 changes: 29 additions & 26 deletions pandas/tests/indexes/ranges/test_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
)
import pandas._testing as tm

# aliases to make some tests easier to read
RI = RangeIndex


class TestRangeIndex:
@pytest.fixture
Expand Down Expand Up @@ -507,25 +504,31 @@ def test_len_specialised(self, step):
@pytest.mark.parametrize(
"indices, expected",
[
([RI(1, 12, 5)], RI(1, 12, 5)),
([RI(0, 6, 4)], RI(0, 6, 4)),
([RI(1, 3), RI(3, 7)], RI(1, 7)),
([RI(1, 5, 2), RI(5, 6)], RI(1, 6, 2)),
([RI(1, 3, 2), RI(4, 7, 3)], RI(1, 7, 3)),
([RI(-4, 3, 2), RI(4, 7, 2)], RI(-4, 7, 2)),
([RI(-4, -8), RI(-8, -12)], RI(0, 0)),
([RI(-4, -8), RI(3, -4)], RI(0, 0)),
([RI(-4, -8), RI(3, 5)], RI(3, 5)),
([RI(-4, -2), RI(3, 5)], Index([-4, -3, 3, 4])),
([RI(-2), RI(3, 5)], RI(3, 5)),
([RI(2), RI(2)], Index([0, 1, 0, 1])),
([RI(2), RI(2, 5), RI(5, 8, 4)], RI(0, 6)),
([RI(2), RI(3, 5), RI(5, 8, 4)], Index([0, 1, 3, 4, 5])),
([RI(-2, 2), RI(2, 5), RI(5, 8, 4)], RI(-2, 6)),
([RI(3), Index([-1, 3, 15])], Index([0, 1, 2, -1, 3, 15])),
([RI(3), Index([-1, 3.1, 15.0])], Index([0, 1, 2, -1, 3.1, 15.0])),
([RI(3), Index(["a", None, 14])], Index([0, 1, 2, "a", None, 14])),
([RI(3, 1), Index(["a", None, 14])], Index(["a", None, 14])),
([RangeIndex(1, 12, 5)], RangeIndex(1, 12, 5)),
([RangeIndex(0, 6, 4)], RangeIndex(0, 6, 4)),
([RangeIndex(1, 3), RangeIndex(3, 7)], RangeIndex(1, 7)),
([RangeIndex(1, 5, 2), RangeIndex(5, 6)], RangeIndex(1, 6, 2)),
([RangeIndex(1, 3, 2), RangeIndex(4, 7, 3)], RangeIndex(1, 7, 3)),
([RangeIndex(-4, 3, 2), RangeIndex(4, 7, 2)], RangeIndex(-4, 7, 2)),
([RangeIndex(-4, -8), RangeIndex(-8, -12)], RangeIndex(0, 0)),
([RangeIndex(-4, -8), RangeIndex(3, -4)], RangeIndex(0, 0)),
([RangeIndex(-4, -8), RangeIndex(3, 5)], RangeIndex(3, 5)),
([RangeIndex(-4, -2), RangeIndex(3, 5)], Index([-4, -3, 3, 4])),
([RangeIndex(-2), RangeIndex(3, 5)], RangeIndex(3, 5)),
([RangeIndex(2), RangeIndex(2)], Index([0, 1, 0, 1])),
([RangeIndex(2), RangeIndex(2, 5), RangeIndex(5, 8, 4)], RangeIndex(0, 6)),
(
[RangeIndex(2), RangeIndex(3, 5), RangeIndex(5, 8, 4)],
Index([0, 1, 3, 4, 5]),
),
(
[RangeIndex(-2, 2), RangeIndex(2, 5), RangeIndex(5, 8, 4)],
RangeIndex(-2, 6),
),
([RangeIndex(3), Index([-1, 3, 15])], Index([0, 1, 2, -1, 3, 15])),
([RangeIndex(3), Index([-1, 3.1, 15.0])], Index([0, 1, 2, -1, 3.1, 15.0])),
([RangeIndex(3), Index(["a", None, 14])], Index([0, 1, 2, "a", None, 14])),
([RangeIndex(3, 1), Index(["a", None, 14])], Index(["a", None, 14])),
],
)
def test_append(self, indices, expected):
Expand Down Expand Up @@ -567,7 +570,7 @@ def test_format_empty(self):
assert empty_idx.format(name=True) == [""]

@pytest.mark.parametrize(
"RI",
"ri",
[
RangeIndex(0, -1, -1),
RangeIndex(0, 1, 1),
Expand All @@ -576,10 +579,10 @@ def test_format_empty(self):
RangeIndex(-3, -5, -2),
],
)
def test_append_len_one(self, RI):
def test_append_len_one(self, ri):
# GH39401
result = RI.append([])
tm.assert_index_equal(result, RI, exact=True)
result = ri.append([])
tm.assert_index_equal(result, ri, exact=True)

@pytest.mark.parametrize("base", [RangeIndex(0, 2), Index([0, 1])])
def test_isin_range(self, base):
Expand Down
11 changes: 7 additions & 4 deletions pandas/tests/indexing/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
Timestamp,
)
import pandas._testing as tm
from pandas.api.types import CategoricalDtype as CDT


@pytest.fixture
Expand All @@ -25,7 +24,9 @@ def df():
{
"A": np.arange(6, dtype="int64"),
},
index=CategoricalIndex(list("aabbca"), dtype=CDT(list("cab")), name="B"),
index=CategoricalIndex(
list("aabbca"), dtype=CategoricalDtype(list("cab")), name="B"
),
)


Expand All @@ -35,13 +36,15 @@ def df2():
{
"A": np.arange(6, dtype="int64"),
},
index=CategoricalIndex(list("aabbca"), dtype=CDT(list("cabe")), name="B"),
index=CategoricalIndex(
list("aabbca"), dtype=CategoricalDtype(list("cabe")), name="B"
),
)


class TestCategoricalIndex:
def test_loc_scalar(self, df):
dtype = CDT(list("cab"))
dtype = CategoricalDtype(list("cab"))
result = df.loc["a"]
bidx = Series(list("aaa"), name="B").astype(dtype)
assert bidx.dtype == dtype
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/indexing/test_chaining_and_caching.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from string import ascii_letters as letters
from string import ascii_letters

import numpy as np
import pytest
Expand All @@ -24,9 +24,9 @@

def random_text(nobs=100):
# Construct a DataFrame where each row is a random slice from 'letters'
idxs = np.random.default_rng(2).integers(len(letters), size=(nobs, 2))
idxs = np.random.default_rng(2).integers(len(ascii_letters), size=(nobs, 2))
idxs.sort(axis=1)
strings = [letters[x[0] : x[1]] for x in idxs]
strings = [ascii_letters[x[0] : x[1]] for x in idxs]

return DataFrame(strings, columns=["letters"])

Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/io/formats/test_info.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from io import StringIO
import re
from string import ascii_uppercase as uppercase
from string import ascii_uppercase
import sys
import textwrap

Expand Down Expand Up @@ -452,9 +452,9 @@ def memory_usage(f):
return f.memory_usage(deep=True).sum()

N = 100
M = len(uppercase)
M = len(ascii_uppercase)
index = MultiIndex.from_product(
[list(uppercase), date_range("20160101", periods=N)],
[list(ascii_uppercase), date_range("20160101", periods=N)],
names=["id", "date"],
)
df = DataFrame(
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/io/formats/test_series_info.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from io import StringIO
from string import ascii_uppercase as uppercase
from string import ascii_uppercase
import textwrap

import numpy as np
Expand Down Expand Up @@ -165,9 +165,9 @@ def test_info_memory_usage_bug_on_multiindex():
# GH 14308
# memory usage introspection should not materialize .values
N = 100
M = len(uppercase)
M = len(ascii_uppercase)
index = MultiIndex.from_product(
[list(uppercase), date_range("20160101", periods=N)],
[list(ascii_uppercase), date_range("20160101", periods=N)],
names=["id", "date"],
)
s = Series(np.random.default_rng(2).standard_normal(N * M), index=index)
Expand Down
18 changes: 11 additions & 7 deletions pandas/tests/reshape/merge/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
TimedeltaIndex,
)
import pandas._testing as tm
from pandas.api.types import CategoricalDtype as CDT
from pandas.core.reshape.concat import concat
from pandas.core.reshape.merge import (
MergeError,
Expand Down Expand Up @@ -1842,7 +1841,7 @@ def left():
{
"X": Series(
np.random.default_rng(2).choice(["foo", "bar"], size=(10,))
).astype(CDT(["foo", "bar"])),
).astype(CategoricalDtype(["foo", "bar"])),
"Y": np.random.default_rng(2).choice(["one", "two", "three"], size=(10,)),
}
)
Expand All @@ -1851,7 +1850,10 @@ def left():
@pytest.fixture
def right():
return DataFrame(
{"X": Series(["foo", "bar"]).astype(CDT(["foo", "bar"])), "Z": [1, 2]}
{
"X": Series(["foo", "bar"]).astype(CategoricalDtype(["foo", "bar"])),
"Z": [1, 2],
}
)


Expand Down Expand Up @@ -2002,8 +2004,8 @@ def test_other_columns(self, left, right):
"change",
[
lambda x: x,
lambda x: x.astype(CDT(["foo", "bar", "bah"])),
lambda x: x.astype(CDT(ordered=True)),
lambda x: x.astype(CategoricalDtype(["foo", "bar", "bah"])),
lambda x: x.astype(CategoricalDtype(ordered=True)),
],
)
def test_dtype_on_merged_different(self, change, join_type, left, right):
Expand Down Expand Up @@ -2110,11 +2112,13 @@ def test_merging_with_bool_or_int_cateorical_column(
# GH 17187
# merging with a boolean/int categorical column
df1 = DataFrame({"id": [1, 2, 3, 4], "cat": category_column})
df1["cat"] = df1["cat"].astype(CDT(categories, ordered=ordered))
df1["cat"] = df1["cat"].astype(CategoricalDtype(categories, ordered=ordered))
df2 = DataFrame({"id": [2, 4], "num": [1, 9]})
result = df1.merge(df2)
expected = DataFrame({"id": [2, 4], "cat": expected_categories, "num": [1, 9]})
expected["cat"] = expected["cat"].astype(CDT(categories, ordered=ordered))
expected["cat"] = expected["cat"].astype(
CategoricalDtype(categories, ordered=ordered)
)
tm.assert_frame_equal(expected, result)

def test_merge_on_int_array(self):
Expand Down
12 changes: 6 additions & 6 deletions pandas/tests/reshape/test_cut.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
to_datetime,
)
import pandas._testing as tm
from pandas.api.types import CategoricalDtype as CDT
from pandas.api.types import CategoricalDtype
import pandas.core.reshape.tile as tmod


Expand Down Expand Up @@ -359,7 +359,7 @@ def test_cut_return_intervals():
IntervalIndex.from_breaks(exp_bins, closed="right").take(
[0, 0, 0, 1, 1, 1, 2, 2, 2]
)
).astype(CDT(ordered=True))
).astype(CategoricalDtype(ordered=True))
tm.assert_series_equal(result, expected)


Expand All @@ -370,7 +370,7 @@ def test_series_ret_bins():

expected = Series(
IntervalIndex.from_breaks([-0.003, 1.5, 3], closed="right").repeat(2)
).astype(CDT(ordered=True))
).astype(CategoricalDtype(ordered=True))
tm.assert_series_equal(result, expected)


Expand Down Expand Up @@ -445,7 +445,7 @@ def test_datetime_bin(conv):
Interval(Timestamp(bin_data[1]), Timestamp(bin_data[2])),
]
)
).astype(CDT(ordered=True))
).astype(CategoricalDtype(ordered=True))

bins = [conv(v) for v in bin_data]
result = Series(cut(data, bins=bins))
Expand Down Expand Up @@ -491,7 +491,7 @@ def test_datetime_cut(data):
),
]
)
).astype(CDT(ordered=True))
).astype(CategoricalDtype(ordered=True))
tm.assert_series_equal(Series(result), expected)


Expand Down Expand Up @@ -534,7 +534,7 @@ def test_datetime_tz_cut(bins, box):
),
]
)
).astype(CDT(ordered=True))
).astype(CategoricalDtype(ordered=True))
tm.assert_series_equal(result, expected)


Expand Down
8 changes: 5 additions & 3 deletions pandas/tests/reshape/test_pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
date_range,
)
import pandas._testing as tm
from pandas.api.types import CategoricalDtype as CDT
from pandas.api.types import CategoricalDtype
from pandas.core.reshape import reshape as reshape_lib
from pandas.core.reshape.pivot import pivot_table

Expand Down Expand Up @@ -219,10 +219,12 @@ def test_pivot_table_dropna_categoricals(self, dropna):
}
)

df["A"] = df["A"].astype(CDT(categories, ordered=False))
df["A"] = df["A"].astype(CategoricalDtype(categories, ordered=False))
result = df.pivot_table(index="B", columns="A", values="C", dropna=dropna)
expected_columns = Series(["a", "b", "c"], name="A")
expected_columns = expected_columns.astype(CDT(categories, ordered=False))
expected_columns = expected_columns.astype(
CategoricalDtype(categories, ordered=False)
)
expected_index = Series([1, 2, 3], name="B")
expected = DataFrame(
[[0.0, 3.0, 6.0], [1.0, 4.0, 7.0], [2.0, 5.0, 8.0]],
Expand Down
Loading