Skip to content

CLN: update imports in tests #29275

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

Merged
merged 2 commits into from
Oct 31, 2019
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
65 changes: 31 additions & 34 deletions pandas/tests/scalar/period/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import pandas as pd
from pandas import NaT, Period, Timedelta, Timestamp, offsets
import pandas.core.indexes.period as period
import pandas.util.testing as tm


Expand Down Expand Up @@ -942,7 +941,7 @@ def test_equal(self):
assert self.january1 == self.january2

def test_equal_Raises_Value(self):
with pytest.raises(period.IncompatibleFrequency):
with pytest.raises(IncompatibleFrequency):
self.january1 == self.day

def test_notEqual(self):
Expand All @@ -953,7 +952,7 @@ def test_greater(self):
assert self.february > self.january1

def test_greater_Raises_Value(self):
with pytest.raises(period.IncompatibleFrequency):
with pytest.raises(IncompatibleFrequency):
self.january1 > self.day

def test_greater_Raises_Type(self):
Expand All @@ -964,7 +963,7 @@ def test_greaterEqual(self):
assert self.january1 >= self.january2

def test_greaterEqual_Raises_Value(self):
with pytest.raises(period.IncompatibleFrequency):
with pytest.raises(IncompatibleFrequency):
self.january1 >= self.day

with pytest.raises(TypeError):
Expand All @@ -974,7 +973,7 @@ def test_smallerEqual(self):
assert self.january1 <= self.january2

def test_smallerEqual_Raises_Value(self):
with pytest.raises(period.IncompatibleFrequency):
with pytest.raises(IncompatibleFrequency):
self.january1 <= self.day

def test_smallerEqual_Raises_Type(self):
Expand All @@ -985,7 +984,7 @@ def test_smaller(self):
assert self.january1 < self.february

def test_smaller_Raises_Value(self):
with pytest.raises(period.IncompatibleFrequency):
with pytest.raises(IncompatibleFrequency):
self.january1 < self.day

def test_smaller_Raises_Type(self):
Expand Down Expand Up @@ -1026,7 +1025,7 @@ def test_sub_delta(self):
result = left - right
assert result == 4 * right.freq

with pytest.raises(period.IncompatibleFrequency):
with pytest.raises(IncompatibleFrequency):
left - Period("2007-01", freq="M")

def test_add_integer(self):
Expand Down Expand Up @@ -1097,16 +1096,16 @@ def test_sub(self):
assert per2 - per1 == 14 * off

msg = r"Input has different freq=M from Period\(freq=D\)"
with pytest.raises(period.IncompatibleFrequency, match=msg):
with pytest.raises(IncompatibleFrequency, match=msg):
per1 - Period("2011-02", freq="M")

@pytest.mark.parametrize("n", [1, 2, 3, 4])
def test_sub_n_gt_1_ticks(self, tick_classes, n):
# GH 23878
p1 = pd.Period("19910905", freq=tick_classes(n))
Copy link
Member

Choose a reason for hiding this comment

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

I think doing this will conflict with what @SaturnFromTitan is or will be doing in other PRs. I think better to keep as pd.Period

Copy link
Contributor

Choose a reason for hiding this comment

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

We didn't agree on a solution for this yet - so I don't mind. For now, I'm only focussing on imports from pandas.util.testing

Copy link
Member Author

Choose a reason for hiding this comment

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

As long as Period is already in the namespace, I think this is a strict improvement. If later this gets reverted to only-pd.Period I'm mostly fine with that too.

One reason I like the not-pd pattern is that I can look at imports and get an idea of what is tested in this module.

Copy link
Contributor

Choose a reason for hiding this comment

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

well i am +1 on not using the pd namespace except in a very few cases in tests (eg when we are actually testing the ls namespace)

we should simply use the

from pandas import Series...

pattern it is immediately obvious what is and what is not imported
and there is no confusion about what to use

Copy link
Member

Choose a reason for hiding this comment

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

This is getting into personal development preferences but as a counter argument I sometimes find these imports very annoying. When debugging the pd namespace is almost always available, yet when copy pasting blocks of code that use this import machinery you then have to go up and see what is imported from top of the module.

But that aside... the bigger point is that (beyond tests, where this probably doesn’t matter) the from ... imports cause circular references and we will never be able to enforce those consistently. We end up in a weird state like #29133 where there isn’t a good rhyme or reason to where we place imports except for the fact that we tried it at one point and it caused a circular ref, so when then moved it somewhere else.

Ultimately I hope we don’t have to think about imports or circular references at all

Copy link
Contributor

Choose a reason for hiding this comment

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

it’s not personal preference but just like we avoid * imports
having a global space is a bad idea

explicit is always better than implicit

Copy link
Member Author

Choose a reason for hiding this comment

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

Ultimately I hope we don’t have to think about imports or circular references at all

+1. Fortunately that's not an issue for most of the tests.

p2 = pd.Period("19920406", freq=tick_classes(n))
p1 = Period("19910905", freq=tick_classes(n))
p2 = Period("19920406", freq=tick_classes(n))

expected = pd.Period(str(p2), freq=p2.freq.base) - pd.Period(
expected = Period(str(p2), freq=p2.freq.base) - Period(
str(p1), freq=p1.freq.base
)

Expand All @@ -1117,23 +1116,21 @@ def test_sub_n_gt_1_ticks(self, tick_classes, n):
@pytest.mark.parametrize(
"offset, kwd_name",
[
(pd.offsets.YearEnd, "month"),
(pd.offsets.QuarterEnd, "startingMonth"),
(pd.offsets.MonthEnd, None),
(pd.offsets.Week, "weekday"),
(offsets.YearEnd, "month"),
(offsets.QuarterEnd, "startingMonth"),
(offsets.MonthEnd, None),
(offsets.Week, "weekday"),
],
)
def test_sub_n_gt_1_offsets(self, offset, kwd_name, n, normalize):
# GH 23878
kwds = {kwd_name: 3} if kwd_name is not None else {}
p1_d = "19910905"
p2_d = "19920406"
p1 = pd.Period(p1_d, freq=offset(n, normalize, **kwds))
p2 = pd.Period(p2_d, freq=offset(n, normalize, **kwds))
p1 = Period(p1_d, freq=offset(n, normalize, **kwds))
p2 = Period(p2_d, freq=offset(n, normalize, **kwds))

expected = pd.Period(p2_d, freq=p2.freq.base) - pd.Period(
p1_d, freq=p1.freq.base
)
expected = Period(p2_d, freq=p2.freq.base) - Period(p1_d, freq=p1.freq.base)

assert (p2 - p1) == expected

Expand All @@ -1152,14 +1149,14 @@ def test_add_offset(self):
np.timedelta64(365, "D"),
timedelta(365),
]:
with pytest.raises(period.IncompatibleFrequency):
with pytest.raises(IncompatibleFrequency):
p + o

if isinstance(o, np.timedelta64):
with pytest.raises(TypeError):
o + p
else:
with pytest.raises(period.IncompatibleFrequency):
with pytest.raises(IncompatibleFrequency):
o + p

for freq in ["M", "2M", "3M"]:
Expand All @@ -1179,14 +1176,14 @@ def test_add_offset(self):
np.timedelta64(365, "D"),
timedelta(365),
]:
with pytest.raises(period.IncompatibleFrequency):
with pytest.raises(IncompatibleFrequency):
p + o

if isinstance(o, np.timedelta64):
with pytest.raises(TypeError):
o + p
else:
with pytest.raises(period.IncompatibleFrequency):
with pytest.raises(IncompatibleFrequency):
o + p

# freq is Tick
Expand Down Expand Up @@ -1226,14 +1223,14 @@ def test_add_offset(self):
np.timedelta64(4, "h"),
timedelta(hours=23),
]:
with pytest.raises(period.IncompatibleFrequency):
with pytest.raises(IncompatibleFrequency):
p + o

if isinstance(o, np.timedelta64):
with pytest.raises(TypeError):
o + p
else:
with pytest.raises(period.IncompatibleFrequency):
with pytest.raises(IncompatibleFrequency):
o + p

for freq in ["H", "2H", "3H"]:
Expand Down Expand Up @@ -1272,14 +1269,14 @@ def test_add_offset(self):
np.timedelta64(3200, "s"),
timedelta(hours=23, minutes=30),
]:
with pytest.raises(period.IncompatibleFrequency):
with pytest.raises(IncompatibleFrequency):
p + o

if isinstance(o, np.timedelta64):
with pytest.raises(TypeError):
o + p
else:
with pytest.raises(period.IncompatibleFrequency):
with pytest.raises(IncompatibleFrequency):
o + p

def test_add_offset_nat(self):
Expand Down Expand Up @@ -1376,7 +1373,7 @@ def test_sub_offset(self):
np.timedelta64(365, "D"),
timedelta(365),
]:
with pytest.raises(period.IncompatibleFrequency):
with pytest.raises(IncompatibleFrequency):
p - o

for freq in ["M", "2M", "3M"]:
Expand All @@ -1391,7 +1388,7 @@ def test_sub_offset(self):
np.timedelta64(365, "D"),
timedelta(365),
]:
with pytest.raises(period.IncompatibleFrequency):
with pytest.raises(IncompatibleFrequency):
p - o

# freq is Tick
Expand All @@ -1411,7 +1408,7 @@ def test_sub_offset(self):
np.timedelta64(4, "h"),
timedelta(hours=23),
]:
with pytest.raises(period.IncompatibleFrequency):
with pytest.raises(IncompatibleFrequency):
p - o

for freq in ["H", "2H", "3H"]:
Expand All @@ -1434,7 +1431,7 @@ def test_sub_offset(self):
np.timedelta64(3200, "s"),
timedelta(hours=23, minutes=30),
]:
with pytest.raises(period.IncompatibleFrequency):
with pytest.raises(IncompatibleFrequency):
p - o

def test_sub_offset_nat(self):
Expand Down Expand Up @@ -1530,10 +1527,10 @@ def test_period_ops_offset(self):
assert result == exp

msg = r"Input cannot be converted to Period\(freq=D\)"
with pytest.raises(period.IncompatibleFrequency, match=msg):
with pytest.raises(IncompatibleFrequency, match=msg):
p + offsets.Hour(2)

with pytest.raises(period.IncompatibleFrequency, match=msg):
with pytest.raises(IncompatibleFrequency, match=msg):
p - offsets.Hour(2)


Expand Down
28 changes: 14 additions & 14 deletions pandas/tests/scalar/timedelta/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import pytest

import pandas as pd
from pandas import NaT, Timedelta, Timestamp
from pandas import NaT, Timedelta, Timestamp, offsets
from pandas.core import ops
import pandas.util.testing as tm

Expand All @@ -28,7 +28,7 @@ class TestTimedeltaAdditionSubtraction:
timedelta(seconds=10),
np.timedelta64(10, "s"),
np.timedelta64(10000000000, "ns"),
pd.offsets.Second(10),
offsets.Second(10),
],
)
def test_td_add_sub_ten_seconds(self, ten_seconds):
Expand All @@ -50,7 +50,7 @@ def test_td_add_sub_ten_seconds(self, ten_seconds):
Timedelta("1 days, 00:00:10"),
timedelta(days=1, seconds=10),
np.timedelta64(1, "D") + np.timedelta64(10, "s"),
pd.offsets.Day() + pd.offsets.Second(10),
offsets.Day() + offsets.Second(10),
],
)
def test_td_add_sub_one_day_ten_seconds(self, one_day_ten_secs):
Expand Down Expand Up @@ -114,7 +114,7 @@ def test_td_add_timedelta64(self, op):
def test_td_add_offset(self, op):
td = Timedelta(10, unit="d")

result = op(td, pd.offsets.Hour(6))
result = op(td, offsets.Hour(6))
assert isinstance(result, Timedelta)
assert result == Timedelta(days=10, hours=6)

Expand Down Expand Up @@ -167,7 +167,7 @@ def test_td_sub_td64_nat(self):

def test_td_sub_offset(self):
td = Timedelta(10, unit="d")
result = td - pd.offsets.Hour(1)
result = td - offsets.Hour(1)
assert isinstance(result, Timedelta)
assert result == Timedelta(239, unit="h")

Expand All @@ -192,7 +192,7 @@ def test_td_rsub_nat(self):
assert result is NaT

def test_td_rsub_offset(self):
result = pd.offsets.Hour(1) - Timedelta(10, unit="d")
result = offsets.Hour(1) - Timedelta(10, unit="d")
assert isinstance(result, Timedelta)
assert result == Timedelta(-239, unit="h")

Expand Down Expand Up @@ -306,7 +306,7 @@ def test_td_div_timedeltalike_scalar(self):
# GH#19738
td = Timedelta(10, unit="d")

result = td / pd.offsets.Hour(1)
result = td / offsets.Hour(1)
assert result == 240

assert td / td == 1
Expand Down Expand Up @@ -342,7 +342,7 @@ def test_td_div_nan(self, nan):
def test_td_rdiv_timedeltalike_scalar(self):
# GH#19738
td = Timedelta(10, unit="d")
result = pd.offsets.Hour(1) / td
result = offsets.Hour(1) / td
assert result == 1 / 240.0

assert np.timedelta64(60, "h") / td == 0.25
Expand Down Expand Up @@ -370,8 +370,8 @@ def test_td_floordiv_null_scalar(self):
def test_td_floordiv_offsets(self):
# GH#19738
td = Timedelta(hours=3, minutes=4)
assert td // pd.offsets.Hour(1) == 3
assert td // pd.offsets.Minute(2) == 92
assert td // offsets.Hour(1) == 3
assert td // offsets.Minute(2) == 92

def test_td_floordiv_invalid_scalar(self):
# GH#18846
Expand Down Expand Up @@ -441,7 +441,7 @@ def test_td_rfloordiv_null_scalar(self):

def test_td_rfloordiv_offsets(self):
# GH#19738
assert pd.offsets.Hour(1) // Timedelta(minutes=25) == 2
assert offsets.Hour(1) // Timedelta(minutes=25) == 2

def test_td_rfloordiv_invalid_scalar(self):
# GH#18846
Expand Down Expand Up @@ -532,7 +532,7 @@ def test_mod_offset(self):
# GH#19365
td = Timedelta(hours=37)

result = td % pd.offsets.Hour(5)
result = td % offsets.Hour(5)
assert isinstance(result, Timedelta)
assert result == Timedelta(hours=2)

Expand Down Expand Up @@ -633,7 +633,7 @@ def test_divmod_offset(self):
# GH#19365
td = Timedelta(days=2, hours=6)

result = divmod(td, pd.offsets.Hour(-4))
result = divmod(td, offsets.Hour(-4))
assert result[0] == -14
assert isinstance(result[1], Timedelta)
assert result[1] == Timedelta(hours=-2)
Expand All @@ -653,7 +653,7 @@ def test_rdivmod_pytimedelta(self):
assert result[1] == Timedelta(hours=6)

def test_rdivmod_offset(self):
result = divmod(pd.offsets.Hour(54), Timedelta(hours=-4))
result = divmod(offsets.Hour(54), Timedelta(hours=-4))
assert result[0] == -14
assert isinstance(result[1], Timedelta)
assert result[1] == Timedelta(hours=-2)
Expand Down
Loading