Skip to content

Commit 35a92c7

Browse files
jbrockmendelukarroum
authored andcommitted
TST/REF: collect tests from test_api into method-specific files (pandas-dev#37525)
1 parent 57614eb commit 35a92c7

File tree

10 files changed

+96
-102
lines changed

10 files changed

+96
-102
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import pytest
2+
3+
from pandas import DataFrame
4+
import pandas._testing as tm
5+
6+
7+
class TestCopy:
8+
@pytest.mark.parametrize("attr", ["index", "columns"])
9+
def test_copy_index_name_checking(self, float_frame, attr):
10+
# don't want to be able to modify the index stored elsewhere after
11+
# making a copy
12+
ind = getattr(float_frame, attr)
13+
ind.name = None
14+
cp = float_frame.copy()
15+
getattr(cp, attr).name = "foo"
16+
assert getattr(float_frame, attr).name is None
17+
18+
def test_copy_cache(self):
19+
# GH#31784 _item_cache not cleared on copy causes incorrect reads after updates
20+
df = DataFrame({"a": [1]})
21+
22+
df["x"] = [0]
23+
df["a"]
24+
25+
df.copy()
26+
27+
df["a"].values[0] = -1
28+
29+
tm.assert_frame_equal(df, DataFrame({"a": [-1], "x": [0]}))
30+
31+
df["y"] = [0]
32+
33+
assert df["a"].values[0] == -1
34+
tm.assert_frame_equal(df, DataFrame({"a": [-1], "x": [0], "y": [0]}))
35+
36+
def test_copy(self, float_frame, float_string_frame):
37+
cop = float_frame.copy()
38+
cop["E"] = cop["A"]
39+
assert "E" not in float_frame
40+
41+
# copy objects
42+
copy = float_string_frame.copy()
43+
assert copy._mgr is not float_string_frame._mgr
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import numpy as np
2+
3+
from pandas import DataFrame, Timestamp
4+
import pandas._testing as tm
5+
6+
7+
class TestToNumpy:
8+
def test_to_numpy(self):
9+
df = DataFrame({"A": [1, 2], "B": [3, 4.5]})
10+
expected = np.array([[1, 3], [2, 4.5]])
11+
result = df.to_numpy()
12+
tm.assert_numpy_array_equal(result, expected)
13+
14+
def test_to_numpy_dtype(self):
15+
df = DataFrame({"A": [1, 2], "B": [3, 4.5]})
16+
expected = np.array([[1, 3], [2, 4]], dtype="int64")
17+
result = df.to_numpy(dtype="int64")
18+
tm.assert_numpy_array_equal(result, expected)
19+
20+
def test_to_numpy_copy(self):
21+
arr = np.random.randn(4, 3)
22+
df = DataFrame(arr)
23+
assert df.values.base is arr
24+
assert df.to_numpy(copy=False).base is arr
25+
assert df.to_numpy(copy=True).base is not arr
26+
27+
def test_to_numpy_mixed_dtype_to_str(self):
28+
# https://github.com/pandas-dev/pandas/issues/35455
29+
df = DataFrame([[Timestamp("2020-01-01 00:00:00"), 100.0]])
30+
result = df.to_numpy(dtype=str)
31+
expected = np.array([["2020-01-01 00:00:00", "100.0"]], dtype=str)
32+
tm.assert_numpy_array_equal(result, expected)

pandas/tests/frame/test_api.py

Lines changed: 2 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,6 @@
1717

1818

1919
class TestDataFrameMisc:
20-
@pytest.mark.parametrize("attr", ["index", "columns"])
21-
def test_copy_index_name_checking(self, float_frame, attr):
22-
# don't want to be able to modify the index stored elsewhere after
23-
# making a copy
24-
ind = getattr(float_frame, attr)
25-
ind.name = None
26-
cp = float_frame.copy()
27-
getattr(cp, attr).name = "foo"
28-
assert getattr(float_frame, attr).name is None
29-
3020
def test_getitem_pop_assign_name(self, float_frame):
3121
s = float_frame["A"]
3222
assert s.name == "A"
@@ -87,8 +77,7 @@ def test_get_axis(self, float_frame):
8777
f._get_axis_number(None)
8878

8979
def test_keys(self, float_frame):
90-
getkeys = float_frame.keys
91-
assert getkeys() is float_frame.columns
80+
assert float_frame.keys() is float_frame.columns
9281

9382
def test_column_contains_raises(self, float_frame):
9483
with pytest.raises(TypeError, match="unhashable type: 'Index'"):
@@ -137,15 +126,6 @@ def test_new_empty_index(self):
137126
df1.index.name = "foo"
138127
assert df2.index.name is None
139128

140-
def test_array_interface(self, float_frame):
141-
with np.errstate(all="ignore"):
142-
result = np.sqrt(float_frame)
143-
assert isinstance(result, type(float_frame))
144-
assert result.index is float_frame.index
145-
assert result.columns is float_frame.columns
146-
147-
tm.assert_frame_equal(result, float_frame.apply(np.sqrt))
148-
149129
def test_get_agg_axis(self, float_frame):
150130
cols = float_frame._get_agg_axis(0)
151131
assert cols is float_frame.columns
@@ -157,7 +137,7 @@ def test_get_agg_axis(self, float_frame):
157137
with pytest.raises(ValueError, match=msg):
158138
float_frame._get_agg_axis(2)
159139

160-
def test_nonzero(self, float_frame, float_string_frame):
140+
def test_empty(self, float_frame, float_string_frame):
161141
empty_frame = DataFrame()
162142
assert empty_frame.empty
163143

@@ -314,32 +294,6 @@ def test_len(self, float_frame):
314294
expected = float_frame.reindex(columns=["A", "B"]).values
315295
tm.assert_almost_equal(arr, expected)
316296

317-
def test_to_numpy(self):
318-
df = DataFrame({"A": [1, 2], "B": [3, 4.5]})
319-
expected = np.array([[1, 3], [2, 4.5]])
320-
result = df.to_numpy()
321-
tm.assert_numpy_array_equal(result, expected)
322-
323-
def test_to_numpy_dtype(self):
324-
df = DataFrame({"A": [1, 2], "B": [3, 4.5]})
325-
expected = np.array([[1, 3], [2, 4]], dtype="int64")
326-
result = df.to_numpy(dtype="int64")
327-
tm.assert_numpy_array_equal(result, expected)
328-
329-
def test_to_numpy_copy(self):
330-
arr = np.random.randn(4, 3)
331-
df = DataFrame(arr)
332-
assert df.values.base is arr
333-
assert df.to_numpy(copy=False).base is arr
334-
assert df.to_numpy(copy=True).base is not arr
335-
336-
def test_to_numpy_mixed_dtype_to_str(self):
337-
# https://github.com/pandas-dev/pandas/issues/35455
338-
df = DataFrame([[pd.Timestamp("2020-01-01 00:00:00"), 100.0]])
339-
result = df.to_numpy(dtype=str)
340-
expected = np.array([["2020-01-01 00:00:00", "100.0"]], dtype=str)
341-
tm.assert_numpy_array_equal(result, expected)
342-
343297
def test_swapaxes(self):
344298
df = DataFrame(np.random.randn(10, 5))
345299
tm.assert_frame_equal(df.T, df.swapaxes(0, 1))
@@ -538,24 +492,6 @@ def test_set_flags(self, allows_duplicate_labels):
538492
result.iloc[0, 0] = 10
539493
assert df.iloc[0, 0] == 0
540494

541-
def test_cache_on_copy(self):
542-
# GH 31784 _item_cache not cleared on copy causes incorrect reads after updates
543-
df = DataFrame({"a": [1]})
544-
545-
df["x"] = [0]
546-
df["a"]
547-
548-
df.copy()
549-
550-
df["a"].values[0] = -1
551-
552-
tm.assert_frame_equal(df, DataFrame({"a": [-1], "x": [0]}))
553-
554-
df["y"] = [0]
555-
556-
assert df["a"].values[0] == -1
557-
tm.assert_frame_equal(df, DataFrame({"a": [-1], "x": [0], "y": [0]}))
558-
559495
@skip_if_no("jinja2")
560496
def test_constructor_expanddim_lookup(self):
561497
# GH#33628 accessing _constructor_expanddim should not

pandas/tests/frame/test_block_internals.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -254,15 +254,6 @@ def f(dtype):
254254
if not compat.is_platform_windows():
255255
f("M8[ns]")
256256

257-
def test_copy(self, float_frame, float_string_frame):
258-
cop = float_frame.copy()
259-
cop["E"] = cop["A"]
260-
assert "E" not in float_frame
261-
262-
# copy objects
263-
copy = float_string_frame.copy()
264-
assert copy._mgr is not float_string_frame._mgr
265-
266257
def test_pickle(self, float_string_frame, timezone_frame):
267258
empty_frame = DataFrame()
268259

pandas/tests/frame/test_npfuncs.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,12 @@ def test_asarray_homogenous(self):
1414
# may change from object in the future
1515
expected = np.array([[1, 1], [2, 2]], dtype="object")
1616
tm.assert_numpy_array_equal(result, expected)
17+
18+
def test_np_sqrt(self, float_frame):
19+
with np.errstate(all="ignore"):
20+
result = np.sqrt(float_frame)
21+
assert isinstance(result, type(float_frame))
22+
assert result.index is float_frame.index
23+
assert result.columns is float_frame.columns
24+
25+
tm.assert_frame_equal(result, float_frame.apply(np.sqrt))
File renamed without changes.

pandas/tests/frame/test_timezones.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,3 @@ def test_boolean_compare_transpose_tzindex_with_dst(self, tz):
5858
result = df.T == df.T
5959
expected = DataFrame(True, index=list("ab"), columns=idx)
6060
tm.assert_frame_equal(result, expected)
61-
62-
@pytest.mark.parametrize("copy", [True, False])
63-
@pytest.mark.parametrize(
64-
"method, tz", [["tz_localize", None], ["tz_convert", "Europe/Berlin"]]
65-
)
66-
def test_tz_localize_convert_copy_inplace_mutate(self, copy, method, tz):
67-
# GH 6326
68-
result = DataFrame(
69-
np.arange(0, 5), index=date_range("20131027", periods=5, freq="1H", tz=tz)
70-
)
71-
getattr(result, method)("UTC", copy=copy)
72-
expected = DataFrame(
73-
np.arange(0, 5), index=date_range("20131027", periods=5, freq="1H", tz=tz)
74-
)
75-
tm.assert_frame_equal(result, expected)

pandas/tests/series/methods/test_append.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88

99
class TestSeriesAppend:
10+
def test_append_preserve_name(self, datetime_series):
11+
result = datetime_series[:5].append(datetime_series[5:])
12+
assert result.name == datetime_series.name
13+
1014
def test_append(self, datetime_series, string_series, object_series):
1115
appended_series = string_series.append(object_series)
1216
for idx, value in appended_series.items():

pandas/tests/series/test_api.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@
1212

1313

1414
class TestSeriesMisc:
15-
def test_append_preserve_name(self, datetime_series):
16-
result = datetime_series[:5].append(datetime_series[5:])
17-
assert result.name == datetime_series.name
18-
1915
def test_getitem_preserve_name(self, datetime_series):
2016
result = datetime_series[datetime_series > 0]
2117
assert result.name == datetime_series.name
@@ -143,10 +139,7 @@ def test_iter_strings(self, string_series):
143139
assert val == string_series[i]
144140

145141
def test_keys(self, datetime_series):
146-
# HACK: By doing this in two stages, we avoid 2to3 wrapping the call
147-
# to .keys() in a list()
148-
getkeys = datetime_series.keys
149-
assert getkeys() is datetime_series.index
142+
assert datetime_series.keys() is datetime_series.index
150143

151144
def test_values(self, datetime_series):
152145
tm.assert_almost_equal(
@@ -193,10 +186,6 @@ def test_class_axis(self):
193186
# no exception and no empty docstring
194187
assert pydoc.getdoc(Series.index)
195188

196-
def test_numpy_unique(self, datetime_series):
197-
# it works!
198-
np.unique(datetime_series)
199-
200189
def test_item(self):
201190
s = Series([1])
202191
result = s.item()

pandas/tests/series/test_duplicates.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ def test_nunique():
2121
assert s.nunique() == 0
2222

2323

24+
def test_numpy_unique(datetime_series):
25+
# it works!
26+
np.unique(datetime_series)
27+
28+
2429
def test_unique():
2530
# GH714 also, dtype=float
2631
s = Series([1.2345] * 100)

0 commit comments

Comments
 (0)