Skip to content

Commit 85621a4

Browse files
authored
TST/REF: collect tests by method (#37395)
1 parent 0e5d004 commit 85621a4

File tree

12 files changed

+147
-146
lines changed

12 files changed

+147
-146
lines changed

pandas/tests/frame/indexing/test_setitem.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
Interval,
1111
NaT,
1212
Period,
13+
PeriodIndex,
1314
Series,
1415
Timestamp,
1516
date_range,
1617
notna,
18+
period_range,
1719
)
1820
import pandas._testing as tm
1921
from pandas.core.arrays import SparseArray
@@ -213,3 +215,17 @@ def test_setitem_dt64tz(self, timezone_frame):
213215
result = df2["B"]
214216
tm.assert_series_equal(notna(result), Series([True, False, True], name="B"))
215217
tm.assert_series_equal(df2.dtypes, df.dtypes)
218+
219+
def test_setitem_periodindex(self):
220+
rng = period_range("1/1/2000", periods=5, name="index")
221+
df = DataFrame(np.random.randn(5, 3), index=rng)
222+
223+
df["Index"] = rng
224+
rs = Index(df["Index"])
225+
tm.assert_index_equal(rs, rng, check_names=False)
226+
assert rs.name == "Index"
227+
assert rng.name == "index"
228+
229+
rs = df.reset_index().set_index("index")
230+
assert isinstance(rs.index, PeriodIndex)
231+
tm.assert_index_equal(rs.index, rng)

pandas/tests/frame/test_period.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import numpy as np
22

3-
from pandas import DataFrame, Index, PeriodIndex, period_range
3+
from pandas import DataFrame, period_range
44
import pandas._testing as tm
55

66

@@ -17,17 +17,3 @@ def test_as_frame_columns(self):
1717

1818
ts = df["1/1/2000"]
1919
tm.assert_series_equal(ts, df.iloc[:, 0])
20-
21-
def test_frame_setitem(self):
22-
rng = period_range("1/1/2000", periods=5, name="index")
23-
df = DataFrame(np.random.randn(5, 3), index=rng)
24-
25-
df["Index"] = rng
26-
rs = Index(df["Index"])
27-
tm.assert_index_equal(rs, rng, check_names=False)
28-
assert rs.name == "Index"
29-
assert rng.name == "index"
30-
31-
rs = df.reset_index().set_index("index")
32-
assert isinstance(rs.index, PeriodIndex)
33-
tm.assert_index_equal(rs.index, rng)

pandas/tests/groupby/test_counting.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,20 @@ def test_count_groupby_column_with_nan_in_groupby_column(self):
241241
)
242242
tm.assert_frame_equal(expected, res)
243243

244+
def test_groupby_count_dateparseerror(self):
245+
dr = date_range(start="1/1/2012", freq="5min", periods=10)
246+
247+
# BAD Example, datetimes first
248+
ser = Series(np.arange(10), index=[dr, np.arange(10)])
249+
grouped = ser.groupby(lambda x: x[1] % 2 == 0)
250+
result = grouped.count()
251+
252+
ser = Series(np.arange(10), index=[np.arange(10), dr])
253+
grouped = ser.groupby(lambda x: x[0] % 2 == 0)
254+
expected = grouped.count()
255+
256+
tm.assert_series_equal(result, expected)
257+
244258

245259
def test_groupby_timedelta_cython_count():
246260
df = DataFrame(

pandas/tests/series/indexing/test_setitem.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,38 @@ def test_setitem_boolean_different_order(self, string_series):
107107
expected[expected > 0] = 0
108108

109109
tm.assert_series_equal(copy, expected)
110+
111+
112+
class TestSetitemViewCopySemantics:
113+
def test_setitem_invalidates_datetime_index_freq(self):
114+
# GH#24096 altering a datetime64tz Series inplace invalidates the
115+
# `freq` attribute on the underlying DatetimeIndex
116+
117+
dti = date_range("20130101", periods=3, tz="US/Eastern")
118+
ts = dti[1]
119+
ser = Series(dti)
120+
assert ser._values is not dti
121+
assert ser._values._data.base is not dti._data._data.base
122+
assert dti.freq == "D"
123+
ser.iloc[1] = NaT
124+
assert ser._values.freq is None
125+
126+
# check that the DatetimeIndex was not altered in place
127+
assert ser._values is not dti
128+
assert ser._values._data.base is not dti._data._data.base
129+
assert dti[1] == ts
130+
assert dti.freq == "D"
131+
132+
def test_dt64tz_setitem_does_not_mutate_dti(self):
133+
# GH#21907, GH#24096
134+
dti = date_range("2016-01-01", periods=10, tz="US/Pacific")
135+
ts = dti[0]
136+
ser = Series(dti)
137+
assert ser._values is not dti
138+
assert ser._values._data.base is not dti._data._data.base
139+
assert ser._mgr.blocks[0].values is not dti
140+
assert ser._mgr.blocks[0].values._data.base is not dti._data._data.base
141+
142+
ser[::3] = NaT
143+
assert ser[0] is NaT
144+
assert dti[0] == ts

pandas/tests/series/methods/test_astype.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
CategoricalDtype,
1515
Index,
1616
Interval,
17+
NaT,
1718
Series,
1819
Timedelta,
1920
Timestamp,
@@ -75,6 +76,19 @@ def test_astype_dict_like(self, dtype_class):
7576

7677

7778
class TestAstype:
79+
def test_astype_float_to_period(self):
80+
result = Series([np.nan]).astype("period[D]")
81+
expected = Series([NaT], dtype="period[D]")
82+
tm.assert_series_equal(result, expected)
83+
84+
def test_astype_no_pandas_dtype(self):
85+
# https://github.com/pandas-dev/pandas/pull/24866
86+
ser = Series([1, 2], dtype="int64")
87+
# Don't have PandasDtype in the public API, so we use `.array.dtype`,
88+
# which is a PandasDtype.
89+
result = ser.astype(ser.array.dtype)
90+
tm.assert_series_equal(result, ser)
91+
7892
@pytest.mark.parametrize("dtype", [np.datetime64, np.timedelta64])
7993
def test_astype_generic_timestamp_no_frequency(self, dtype, request):
8094
# see GH#15524, GH#15987
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
We also test Series.notna in this file.
3+
"""
4+
import numpy as np
5+
6+
from pandas import Period, Series
7+
import pandas._testing as tm
8+
9+
10+
class TestIsna:
11+
def test_isna_period_dtype(self):
12+
# GH#13737
13+
ser = Series([Period("2011-01", freq="M"), Period("NaT", freq="M")])
14+
15+
expected = Series([False, True])
16+
17+
result = ser.isna()
18+
tm.assert_series_equal(result, expected)
19+
20+
result = ser.notna()
21+
tm.assert_series_equal(result, ~expected)
22+
23+
def test_isna(self):
24+
ser = Series([0, 5.4, 3, np.nan, -0.001])
25+
expected = Series([False, False, False, True, False])
26+
tm.assert_series_equal(ser.isna(), expected)
27+
tm.assert_series_equal(ser.notna(), ~expected)
28+
29+
ser = Series(["hi", "", np.nan])
30+
expected = Series([False, False, True])
31+
tm.assert_series_equal(ser.isna(), expected)
32+
tm.assert_series_equal(ser.notna(), ~expected)

pandas/tests/series/test_block_internals.py

Lines changed: 0 additions & 39 deletions
This file was deleted.

pandas/tests/series/test_constructors.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
)
3636
import pandas._testing as tm
3737
from pandas.core.arrays import IntervalArray, period_array
38+
from pandas.core.internals.blocks import IntBlock
3839

3940

4041
class TestSeriesConstructors:
@@ -1534,3 +1535,25 @@ def test_series_constructor_datetimelike_index_coercion(self):
15341535
with tm.assert_produces_warning(FutureWarning):
15351536
assert ser.index.is_all_dates
15361537
assert isinstance(ser.index, DatetimeIndex)
1538+
1539+
1540+
class TestSeriesConstructorInternals:
1541+
def test_constructor_no_pandas_array(self):
1542+
ser = Series([1, 2, 3])
1543+
result = Series(ser.array)
1544+
tm.assert_series_equal(ser, result)
1545+
assert isinstance(result._mgr.blocks[0], IntBlock)
1546+
1547+
def test_from_array(self):
1548+
result = Series(pd.array(["1H", "2H"], dtype="timedelta64[ns]"))
1549+
assert result._mgr.blocks[0].is_extension is False
1550+
1551+
result = Series(pd.array(["2015"], dtype="datetime64[ns]"))
1552+
assert result._mgr.blocks[0].is_extension is False
1553+
1554+
def test_from_list_dtype(self):
1555+
result = Series(["1H", "2H"], dtype="timedelta64[ns]")
1556+
assert result._mgr.blocks[0].is_extension is False
1557+
1558+
result = Series(["2015"], dtype="datetime64[ns]")
1559+
assert result._mgr.blocks[0].is_extension is False

pandas/tests/series/test_internals.py

Lines changed: 0 additions & 49 deletions
This file was deleted.

pandas/tests/series/test_missing.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,15 @@ def test_valid(self, datetime_series):
9292
tm.assert_series_equal(result, ts[1::2])
9393
tm.assert_series_equal(result, ts[pd.notna(ts)])
9494

95-
def test_isna(self):
96-
ser = Series([0, 5.4, 3, np.nan, -0.001])
97-
expected = Series([False, False, False, True, False])
98-
tm.assert_series_equal(ser.isna(), expected)
99-
100-
ser = Series(["hi", "", np.nan])
101-
expected = Series([False, False, True])
102-
tm.assert_series_equal(ser.isna(), expected)
103-
104-
def test_notna(self):
105-
ser = Series([0, 5.4, 3, np.nan, -0.001])
106-
expected = Series([True, True, True, False, True])
107-
tm.assert_series_equal(ser.notna(), expected)
108-
109-
ser = Series(["hi", "", np.nan])
110-
expected = Series([True, True, False])
111-
tm.assert_series_equal(ser.notna(), expected)
95+
96+
def test_hasnans_uncached_for_series():
97+
# GH#19700
98+
idx = Index([0, 1])
99+
assert idx.hasnans is False
100+
assert "hasnans" in idx._cache
101+
ser = idx.to_series()
102+
assert ser.hasnans is False
103+
assert not hasattr(ser, "_cache")
104+
ser.iloc[-1] = np.nan
105+
assert ser.hasnans is True
106+
assert Series.hasnans.__doc__ == Index.hasnans.__doc__

pandas/tests/series/test_period.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,12 @@
33

44
import pandas as pd
55
from pandas import DataFrame, Series, period_range
6-
import pandas._testing as tm
76

87

98
class TestSeriesPeriod:
109
def setup_method(self, method):
1110
self.series = Series(period_range("2000-01-01", periods=10, freq="D"))
1211

13-
def test_isna(self):
14-
# GH 13737
15-
s = Series([pd.Period("2011-01", freq="M"), pd.Period("NaT", freq="M")])
16-
tm.assert_series_equal(s.isna(), Series([False, True]))
17-
tm.assert_series_equal(s.notna(), Series([True, False]))
18-
1912
# ---------------------------------------------------------------------
2013
# NaT support
2114

@@ -29,11 +22,6 @@ def test_NaT_scalar(self):
2922
series[2] = val
3023
assert pd.isna(series[2])
3124

32-
def test_NaT_cast(self):
33-
result = Series([np.nan]).astype("period[D]")
34-
expected = Series([pd.NaT], dtype="period[D]")
35-
tm.assert_series_equal(result, expected)
36-
3725
def test_intercept_astype_object(self):
3826
expected = self.series.astype("object")
3927

pandas/tests/series/test_timeseries.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,6 @@ def test_promote_datetime_date(self):
4646
expected = rng.get_indexer(ts_slice.index)
4747
tm.assert_numpy_array_equal(result, expected)
4848

49-
def test_groupby_count_dateparseerror(self):
50-
dr = date_range(start="1/1/2012", freq="5min", periods=10)
51-
52-
# BAD Example, datetimes first
53-
s = Series(np.arange(10), index=[dr, np.arange(10)])
54-
grouped = s.groupby(lambda x: x[1] % 2 == 0)
55-
result = grouped.count()
56-
57-
s = Series(np.arange(10), index=[np.arange(10), dr])
58-
grouped = s.groupby(lambda x: x[0] % 2 == 0)
59-
expected = grouped.count()
60-
61-
tm.assert_series_equal(result, expected)
62-
6349
def test_series_map_box_timedelta(self):
6450
# GH 11349
6551
s = Series(timedelta_range("1 day 1 s", periods=5, freq="h"))

0 commit comments

Comments
 (0)