Skip to content
6 changes: 3 additions & 3 deletions ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ function invgrep {
}

function check_namespace {
local -r CLASS="${1}"
grep -R -l --include "*.py" " ${CLASS}(" pandas/tests | xargs grep -n "pd\.${CLASS}("
local -r CLASS=${1}
grep -R -l --include "*.py" " ${CLASS}(" pandas/tests | xargs grep -n "pd\.${CLASS}[(\.]"
test $? -gt 0
}

Expand Down Expand Up @@ -146,7 +146,7 @@ if [[ -z "$CHECK" || "$CHECK" == "patterns" ]]; then
RET=$(($RET + $?)) ; echo $MSG "DONE"

MSG='Check for inconsistent use of pandas namespace in tests' ; echo $MSG
for class in "Series" "DataFrame" "Index"; do
for class in "Series" "DataFrame" "Index" "MultiIndex" "Timestamp" "Timedelta" "TimedeltaIndex" "DatetimeIndex" "Categorical"; do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn't we move these to pre-commit?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that's been done yet but @MarcoGorelli seemed to think it was doable

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On my to-do list 😇 I hope to get round to it next week

check_namespace ${class}
RET=$(($RET + $?))
done
Expand Down
126 changes: 62 additions & 64 deletions pandas/tests/arithmetic/test_datetime64.py

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions pandas/tests/arithmetic/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def test_mul_td64arr(self, left, box_cls):
right = np.array([1, 2, 3], dtype="m8[s]")
right = box_cls(right)

expected = pd.TimedeltaIndex(["10s", "40s", "90s"])
expected = TimedeltaIndex(["10s", "40s", "90s"])
if isinstance(left, Series) or box_cls is Series:
expected = Series(expected)

Expand All @@ -155,7 +155,7 @@ def test_div_td64arr(self, left, box_cls):
right = np.array([10, 40, 90], dtype="m8[s]")
right = box_cls(right)

expected = pd.TimedeltaIndex(["1s", "2s", "3s"])
expected = TimedeltaIndex(["1s", "2s", "3s"])
if isinstance(left, Series) or box_cls is Series:
expected = Series(expected)

Expand Down Expand Up @@ -189,7 +189,7 @@ def test_numeric_arr_mul_tdscalar(self, scalar_td, numeric_idx, box_with_array):
# GH#19333
box = box_with_array
index = numeric_idx
expected = pd.TimedeltaIndex([pd.Timedelta(days=n) for n in range(len(index))])
expected = TimedeltaIndex([Timedelta(days=n) for n in range(len(index))])

index = tm.box_expected(index, box)
expected = tm.box_expected(expected, box)
Expand Down Expand Up @@ -244,10 +244,10 @@ def test_numeric_arr_rdiv_tdscalar(self, three_days, numeric_idx, box_with_array
@pytest.mark.parametrize(
"other",
[
pd.Timedelta(hours=31),
pd.Timedelta(hours=31).to_pytimedelta(),
pd.Timedelta(hours=31).to_timedelta64(),
pd.Timedelta(hours=31).to_timedelta64().astype("m8[h]"),
Timedelta(hours=31),
Timedelta(hours=31).to_pytimedelta(),
Timedelta(hours=31).to_timedelta64(),
Timedelta(hours=31).to_timedelta64().astype("m8[h]"),
np.timedelta64("NaT"),
np.timedelta64("NaT", "D"),
pd.offsets.Minute(3),
Expand Down
14 changes: 7 additions & 7 deletions pandas/tests/arithmetic/test_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,17 +196,17 @@ def test_mixed_timezone_series_ops_object(self):
# GH#13043
ser = Series(
[
pd.Timestamp("2015-01-01", tz="US/Eastern"),
pd.Timestamp("2015-01-01", tz="Asia/Tokyo"),
Timestamp("2015-01-01", tz="US/Eastern"),
Timestamp("2015-01-01", tz="Asia/Tokyo"),
],
name="xxx",
)
assert ser.dtype == object

exp = Series(
[
pd.Timestamp("2015-01-02", tz="US/Eastern"),
pd.Timestamp("2015-01-02", tz="Asia/Tokyo"),
Timestamp("2015-01-02", tz="US/Eastern"),
Timestamp("2015-01-02", tz="Asia/Tokyo"),
],
name="xxx",
)
Expand All @@ -216,8 +216,8 @@ def test_mixed_timezone_series_ops_object(self):
# object series & object series
ser2 = Series(
[
pd.Timestamp("2015-01-03", tz="US/Eastern"),
pd.Timestamp("2015-01-05", tz="Asia/Tokyo"),
Timestamp("2015-01-03", tz="US/Eastern"),
Timestamp("2015-01-05", tz="Asia/Tokyo"),
],
name="xxx",
)
Expand Down Expand Up @@ -326,7 +326,7 @@ def test_rsub_object(self):
"foo" - index

with pytest.raises(TypeError, match=msg):
np.array([True, pd.Timestamp.now()]) - index
np.array([True, Timestamp.now()]) - index


class MyIndex(pd.Index):
Expand Down
28 changes: 13 additions & 15 deletions pandas/tests/arithmetic/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pandas.errors import PerformanceWarning

import pandas as pd
from pandas import PeriodIndex, Series, TimedeltaIndex, period_range
from pandas import PeriodIndex, Series, Timedelta, TimedeltaIndex, period_range
import pandas._testing as tm
from pandas.core import ops
from pandas.core.arrays import TimedeltaArray
Expand Down Expand Up @@ -41,9 +41,7 @@ def test_compare_zerodim(self, box_with_array):
expected = tm.box_expected(expected, xbox)
tm.assert_equal(result, expected)

@pytest.mark.parametrize(
"scalar", ["foo", pd.Timestamp.now(), pd.Timedelta(days=4)]
)
@pytest.mark.parametrize("scalar", ["foo", Timestamp.now(), Timedelta(days=4)])
def test_compare_invalid_scalar(self, box_with_array, scalar):
# comparison with scalar that cannot be interpreted as a Period
pi = pd.period_range("2000", periods=4)
Expand Down Expand Up @@ -698,9 +696,9 @@ def test_parr_add_sub_float_raises(self, op, other, box_with_array):
"other",
[
# datetime scalars
pd.Timestamp.now(),
pd.Timestamp.now().to_pydatetime(),
pd.Timestamp.now().to_datetime64(),
Timestamp.now(),
Timestamp.now().to_pydatetime(),
Timestamp.now().to_datetime64(),
# datetime-like arrays
pd.date_range("2016-01-01", periods=3, freq="H"),
pd.date_range("2016-01-01", periods=3, tz="Europe/Brussels"),
Expand Down Expand Up @@ -733,7 +731,7 @@ def test_parr_add_sub_invalid(self, other, box_with_array):

def test_pi_add_sub_td64_array_non_tick_raises(self):
rng = pd.period_range("1/1/2000", freq="Q", periods=3)
tdi = pd.TimedeltaIndex(["-1 Day", "-1 Day", "-1 Day"])
tdi = TimedeltaIndex(["-1 Day", "-1 Day", "-1 Day"])
tdarr = tdi.values

msg = r"Cannot add or subtract timedelta64\[ns\] dtype from period\[Q-DEC\]"
Expand All @@ -752,7 +750,7 @@ def test_pi_add_sub_td64_array_tick(self):
# PeriodIndex + Timedelta-like is allowed only with
# tick-like frequencies
rng = pd.period_range("1/1/2000", freq="90D", periods=3)
tdi = pd.TimedeltaIndex(["-1 Day", "-1 Day", "-1 Day"])
tdi = TimedeltaIndex(["-1 Day", "-1 Day", "-1 Day"])
tdarr = tdi.values

expected = pd.period_range("12/31/1999", freq="90D", periods=3)
Expand Down Expand Up @@ -1227,7 +1225,7 @@ def test_parr_add_sub_object_array(self):
pi = pd.period_range("2000-12-31", periods=3, freq="D")
parr = pi.array

other = np.array([pd.Timedelta(days=1), pd.offsets.Day(2), 3])
other = np.array([Timedelta(days=1), pd.offsets.Day(2), 3])

with tm.assert_produces_warning(PerformanceWarning):
result = parr + other
Expand Down Expand Up @@ -1258,10 +1256,10 @@ def test_ops_series_timedelta(self):
name="xxx",
)

result = ser + pd.Timedelta("1 days")
result = ser + Timedelta("1 days")
tm.assert_series_equal(result, expected)

result = pd.Timedelta("1 days") + ser
result = Timedelta("1 days") + ser
tm.assert_series_equal(result, expected)

result = ser + pd.tseries.offsets.Day()
Expand Down Expand Up @@ -1492,7 +1490,7 @@ def test_pi_sub_period(self):
result = np.subtract(pd.Period("2012-01", freq="M"), idx)
tm.assert_index_equal(result, exp)

exp = pd.TimedeltaIndex([np.nan, np.nan, np.nan, np.nan], name="idx")
exp = TimedeltaIndex([np.nan, np.nan, np.nan, np.nan], name="idx")
result = idx - pd.Period("NaT", freq="M")
tm.assert_index_equal(result, exp)
assert result.freq == exp.freq
Expand All @@ -1506,7 +1504,7 @@ def test_pi_sub_pdnat(self):
idx = PeriodIndex(
["2011-01", "2011-02", "NaT", "2011-04"], freq="M", name="idx"
)
exp = pd.TimedeltaIndex([pd.NaT] * 4, name="idx")
exp = TimedeltaIndex([pd.NaT] * 4, name="idx")
tm.assert_index_equal(pd.NaT - idx, exp)
tm.assert_index_equal(idx - pd.NaT, exp)

Expand All @@ -1525,7 +1523,7 @@ def test_pi_sub_period_nat(self):
exp = pd.Index([12 * off, pd.NaT, 10 * off, 9 * off], name="idx")
tm.assert_index_equal(result, exp)

exp = pd.TimedeltaIndex([np.nan, np.nan, np.nan, np.nan], name="idx")
exp = TimedeltaIndex([np.nan, np.nan, np.nan, np.nan], name="idx")
tm.assert_index_equal(idx - pd.Period("NaT", freq="M"), exp)
tm.assert_index_equal(pd.Period("NaT", freq="M") - idx, exp)

Expand Down
34 changes: 15 additions & 19 deletions pandas/tests/arithmetic/test_timedelta64.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def test_td64arr_cmp_arraylike_invalid(self, other):
def test_td64arr_cmp_mixed_invalid(self):
rng = timedelta_range("1 days", periods=5)._data

other = np.array([0, 1, 2, rng[3], pd.Timestamp.now()])
other = np.array([0, 1, 2, rng[3], Timestamp.now()])
result = rng == other
expected = np.array([False, False, False, True, False])
tm.assert_numpy_array_equal(result, expected)
Expand All @@ -143,10 +143,8 @@ class TestTimedelta64ArrayComparisons:

@pytest.mark.parametrize("dtype", [None, object])
def test_comp_nat(self, dtype):
left = pd.TimedeltaIndex(
[pd.Timedelta("1 days"), pd.NaT, pd.Timedelta("3 days")]
)
right = pd.TimedeltaIndex([pd.NaT, pd.NaT, pd.Timedelta("3 days")])
left = TimedeltaIndex([Timedelta("1 days"), pd.NaT, Timedelta("3 days")])
right = TimedeltaIndex([pd.NaT, pd.NaT, Timedelta("3 days")])

lhs, rhs = left, right
if dtype is object:
Expand All @@ -173,7 +171,7 @@ def test_comp_nat(self, dtype):
tm.assert_numpy_array_equal(pd.NaT > lhs, expected)

def test_comparisons_nat(self):
tdidx1 = pd.TimedeltaIndex(
tdidx1 = TimedeltaIndex(
[
"1 day",
pd.NaT,
Expand All @@ -183,7 +181,7 @@ def test_comparisons_nat(self):
"5 day 00:00:03",
]
)
tdidx2 = pd.TimedeltaIndex(
tdidx2 = TimedeltaIndex(
["2 day", "2 day", pd.NaT, pd.NaT, "1 day 00:00:02", "5 days 00:00:03"]
)
tdarr = np.array(
Expand Down Expand Up @@ -1030,7 +1028,7 @@ def test_tdi_sub_dt64_array(self, box_with_array):
dti = pd.date_range("2016-01-01", periods=3)
tdi = dti - dti.shift(1)
dtarr = dti.values
expected = pd.DatetimeIndex(dtarr) - tdi
expected = DatetimeIndex(dtarr) - tdi

tdi = tm.box_expected(tdi, box_with_array)
expected = tm.box_expected(expected, box_with_array)
Expand All @@ -1047,7 +1045,7 @@ def test_tdi_add_dt64_array(self, box_with_array):
dti = pd.date_range("2016-01-01", periods=3)
tdi = dti - dti.shift(1)
dtarr = dti.values
expected = pd.DatetimeIndex(dtarr) + tdi
expected = DatetimeIndex(dtarr) + tdi

tdi = tm.box_expected(tdi, box_with_array)
expected = tm.box_expected(expected, box_with_array)
Expand All @@ -1062,7 +1060,7 @@ def test_td64arr_add_datetime64_nat(self, box_with_array):
other = np.datetime64("NaT")

tdi = timedelta_range("1 day", periods=3)
expected = pd.DatetimeIndex(["NaT", "NaT", "NaT"])
expected = DatetimeIndex(["NaT", "NaT", "NaT"])

tdser = tm.box_expected(tdi, box_with_array)
expected = tm.box_expected(expected, box_with_array)
Expand Down Expand Up @@ -1246,9 +1244,9 @@ def test_td64arr_add_sub_tdi(self, box_with_array, names):
def test_td64arr_add_sub_td64_nat(self, box_with_array):
# GH#23320 special handling for timedelta64("NaT")
box = box_with_array
tdi = pd.TimedeltaIndex([NaT, Timedelta("1s")])
tdi = TimedeltaIndex([NaT, Timedelta("1s")])
other = np.timedelta64("NaT")
expected = pd.TimedeltaIndex(["NaT"] * 2)
expected = TimedeltaIndex(["NaT"] * 2)

obj = tm.box_expected(tdi, box)
expected = tm.box_expected(expected, box)
Expand Down Expand Up @@ -1472,14 +1470,14 @@ def test_td64arr_add_sub_object_array(self, box_with_array):
tdarr = tm.box_expected(tdi, box)

other = np.array(
[pd.Timedelta(days=1), pd.offsets.Day(2), pd.Timestamp("2000-01-04")]
[Timedelta(days=1), pd.offsets.Day(2), Timestamp("2000-01-04")]
)

with tm.assert_produces_warning(PerformanceWarning):
result = tdarr + other

expected = pd.Index(
[pd.Timedelta(days=2), pd.Timedelta(days=4), pd.Timestamp("2000-01-07")]
[Timedelta(days=2), Timedelta(days=4), Timestamp("2000-01-07")]
)
expected = tm.box_expected(expected, xbox)
tm.assert_equal(result, expected)
Expand All @@ -1492,9 +1490,7 @@ def test_td64arr_add_sub_object_array(self, box_with_array):
with tm.assert_produces_warning(PerformanceWarning):
result = other - tdarr

expected = pd.Index(
[pd.Timedelta(0), pd.Timedelta(0), pd.Timestamp("2000-01-01")]
)
expected = pd.Index([Timedelta(0), Timedelta(0), Timestamp("2000-01-01")])
expected = tm.box_expected(expected, xbox)
tm.assert_equal(result, expected)

Expand Down Expand Up @@ -2188,9 +2184,9 @@ def test_td64arr_pow_invalid(self, scalar_td, box_with_array):

def test_add_timestamp_to_timedelta():
# GH: 35897
timestamp = pd.Timestamp.now()
timestamp = Timestamp.now()
result = timestamp + pd.timedelta_range("0s", "1s", periods=31)
expected = pd.DatetimeIndex(
expected = DatetimeIndex(
[
timestamp
+ (
Expand Down
20 changes: 10 additions & 10 deletions pandas/tests/arrays/categorical/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def test_constructor_empty(self):

def test_constructor_empty_boolean(self):
# see gh-22702
cat = pd.Categorical([], categories=[True, False])
cat = Categorical([], categories=[True, False])
categories = sorted(cat.categories.tolist())
assert categories == [False, True]

Expand Down Expand Up @@ -412,7 +412,7 @@ def test_constructor_str_unknown(self):

def test_constructor_np_strs(self):
# GH#31499 Hastable.map_locations needs to work on np.str_ objects
cat = pd.Categorical(["1", "0", "1"], [np.str_("0"), np.str_("1")])
cat = Categorical(["1", "0", "1"], [np.str_("0"), np.str_("1")])
assert all(isinstance(x, np.str_) for x in cat.categories)

def test_constructor_from_categorical_with_dtype(self):
Expand Down Expand Up @@ -637,48 +637,48 @@ def test_constructor_imaginary(self):

def test_constructor_string_and_tuples(self):
# GH 21416
c = pd.Categorical(np.array(["c", ("a", "b"), ("b", "a"), "c"], dtype=object))
c = Categorical(np.array(["c", ("a", "b"), ("b", "a"), "c"], dtype=object))
expected_index = Index([("a", "b"), ("b", "a"), "c"])
assert c.categories.equals(expected_index)

def test_interval(self):
idx = pd.interval_range(0, 10, periods=10)
cat = pd.Categorical(idx, categories=idx)
cat = Categorical(idx, categories=idx)
expected_codes = np.arange(10, dtype="int8")
tm.assert_numpy_array_equal(cat.codes, expected_codes)
tm.assert_index_equal(cat.categories, idx)

# infer categories
cat = pd.Categorical(idx)
cat = Categorical(idx)
tm.assert_numpy_array_equal(cat.codes, expected_codes)
tm.assert_index_equal(cat.categories, idx)

# list values
cat = pd.Categorical(list(idx))
cat = Categorical(list(idx))
tm.assert_numpy_array_equal(cat.codes, expected_codes)
tm.assert_index_equal(cat.categories, idx)

# list values, categories
cat = pd.Categorical(list(idx), categories=list(idx))
cat = Categorical(list(idx), categories=list(idx))
tm.assert_numpy_array_equal(cat.codes, expected_codes)
tm.assert_index_equal(cat.categories, idx)

# shuffled
values = idx.take([1, 2, 0])
cat = pd.Categorical(values, categories=idx)
cat = Categorical(values, categories=idx)
tm.assert_numpy_array_equal(cat.codes, np.array([1, 2, 0], dtype="int8"))
tm.assert_index_equal(cat.categories, idx)

# extra
values = pd.interval_range(8, 11, periods=3)
cat = pd.Categorical(values, categories=idx)
cat = Categorical(values, categories=idx)
expected_codes = np.array([8, 9, -1], dtype="int8")
tm.assert_numpy_array_equal(cat.codes, expected_codes)
tm.assert_index_equal(cat.categories, idx)

# overlapping
idx = pd.IntervalIndex([pd.Interval(0, 2), pd.Interval(0, 1)])
cat = pd.Categorical(idx, categories=idx)
cat = Categorical(idx, categories=idx)
expected_codes = np.array([0, 1], dtype="int8")
tm.assert_numpy_array_equal(cat.codes, expected_codes)
tm.assert_index_equal(cat.categories, idx)
Loading