Skip to content

TST/REF: collect tests by method #38589

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 4 commits into from
Dec 21, 2020
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
9 changes: 0 additions & 9 deletions pandas/tests/indexes/datetimes/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
from pandas import DataFrame, DatetimeIndex, Index, Timestamp, date_range, offsets
import pandas._testing as tm

randn = np.random.randn


class TestDatetimeIndex:
def test_time_loc(self): # GH8667
Expand Down Expand Up @@ -355,10 +353,3 @@ def test_to_frame_datetime_tz(self):
result = idx.to_frame()
expected = DataFrame(idx, index=idx)
tm.assert_frame_equal(result, expected)

def test_split_non_utc(self):
# GH 14042
indices = date_range("2016-01-01 00:00:00+0200", freq="S", periods=10)
result = np.split(indices, indices_or_sections=[])[0]
expected = indices._with_freq(None)
tm.assert_index_equal(result, expected)
13 changes: 13 additions & 0 deletions pandas/tests/indexes/datetimes/test_npfuncs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import numpy as np

from pandas import date_range
import pandas._testing as tm


class TestSplit:
def test_split_non_utc(self):
# GH#14042
indices = date_range("2016-01-01 00:00:00+0200", freq="S", periods=10)
result = np.split(indices, indices_or_sections=[])[0]
expected = indices._with_freq(None)
tm.assert_index_equal(result, expected)
29 changes: 29 additions & 0 deletions pandas/tests/series/indexing/test_getitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@


class TestSeriesGetitemScalars:
def test_getitem_negative_out_of_bounds(self):
ser = Series(tm.rands_array(5, 10), index=tm.rands_array(10, 10))

msg = "index -11 is out of bounds for axis 0 with size 10"
with pytest.raises(IndexError, match=msg):
ser[-11]

def test_getitem_out_of_bounds_indexerror(self, datetime_series):
# don't segfault, GH#495
msg = r"index \d+ is out of bounds for axis 0 with size \d+"
Expand Down Expand Up @@ -186,6 +193,17 @@ def test_getitem_slice_date(self, slc, positions):
expected = ser.take(positions)
tm.assert_series_equal(result, expected)

def test_getitem_slice_float_raises(self, datetime_series):
msg = (
"cannot do slice indexing on DatetimeIndex with these indexers "
r"\[{key}\] of type float"
)
with pytest.raises(TypeError, match=msg.format(key=r"4\.0")):
datetime_series[4.0:10.0]

with pytest.raises(TypeError, match=msg.format(key=r"4\.5")):
datetime_series[4.5:10.0]


class TestSeriesGetitemListLike:
@pytest.mark.parametrize("box", [list, np.array, Index, pd.Series])
Expand Down Expand Up @@ -461,3 +479,14 @@ def test_getitem_1tuple_slice_without_multiindex():
result = ser[key]
expected = ser[key[0]]
tm.assert_series_equal(result, expected)


def test_getitem_preserve_name(datetime_series):
result = datetime_series[datetime_series > 0]
assert result.name == datetime_series.name

result = datetime_series[[0, 2, 4]]
assert result.name == datetime_series.name

result = datetime_series[5:10]
assert result.name == datetime_series.name
28 changes: 0 additions & 28 deletions pandas/tests/series/indexing/test_numeric.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import numpy as np
import pytest

from pandas import DataFrame, Index, Series
import pandas._testing as tm
Expand Down Expand Up @@ -30,16 +29,6 @@ def test_slice_float64():
tm.assert_frame_equal(result, expected)


def test_getitem_negative_out_of_bounds():
s = Series(tm.rands_array(5, 10), index=tm.rands_array(10, 10))

msg = "index -11 is out of bounds for axis 0 with size 10"
with pytest.raises(IndexError, match=msg):
s[-11]
with pytest.raises(IndexError, match=msg):
s[-11] = "foo"


def test_getitem_setitem_slice_bug():
s = Series(range(10), index=list(range(10)))
result = s[-12:]
Expand Down Expand Up @@ -69,20 +58,3 @@ def test_getitem_setitem_slice_integers():
s[:4] = 0
assert (s[:4] == 0).all()
assert not (s[4:] == 0).any()


def test_slice_float_get_set(datetime_series):
msg = (
"cannot do slice indexing on DatetimeIndex with these indexers "
r"\[{key}\] of type float"
)
with pytest.raises(TypeError, match=msg.format(key=r"4\.0")):
datetime_series[4.0:10.0]

with pytest.raises(TypeError, match=msg.format(key=r"4\.0")):
datetime_series[4.0:10.0] = 0

with pytest.raises(TypeError, match=msg.format(key=r"4\.5")):
datetime_series[4.5:10.0]
with pytest.raises(TypeError, match=msg.format(key=r"4\.5")):
datetime_series[4.5:10.0] = 0
24 changes: 23 additions & 1 deletion pandas/tests/series/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
date_range,
period_range,
)
import pandas._testing as tm
from pandas.core.indexing import IndexingError
import pandas.testing as tm

from pandas.tseries.offsets import BDay

Expand Down Expand Up @@ -84,6 +84,28 @@ def test_setitem_na_period_dtype_casts_to_nat(self, na_val):
assert ser[4] is NaT


class TestSetitemScalarIndexer:
def test_setitem_negative_out_of_bounds(self):
ser = Series(tm.rands_array(5, 10), index=tm.rands_array(10, 10))

msg = "index -11 is out of bounds for axis 0 with size 10"
with pytest.raises(IndexError, match=msg):
ser[-11] = "foo"


class TestSetitemSlices:
def test_setitem_slice_float_raises(self, datetime_series):
msg = (
"cannot do slice indexing on DatetimeIndex with these indexers "
r"\[{key}\] of type float"
)
with pytest.raises(TypeError, match=msg.format(key=r"4\.0")):
datetime_series[4.0:10.0] = 0

with pytest.raises(TypeError, match=msg.format(key=r"4\.5")):
datetime_series[4.5:10.0] = 0


class TestSetitemBooleanMask:
def test_setitem_boolean(self, string_series):
mask = string_series > string_series.median()
Expand Down
41 changes: 41 additions & 0 deletions pandas/tests/series/methods/test_is_unique.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import numpy as np
import pytest

from pandas import Series
from pandas.core.construction import create_series_with_explicit_dtype


@pytest.mark.parametrize(
"data, expected",
[
(np.random.randint(0, 10, size=1000), False),
(np.arange(1000), True),
([], True),
([np.nan], True),
(["foo", "bar", np.nan], True),
(["foo", "foo", np.nan], False),
(["foo", "bar", np.nan, np.nan], False),
],
)
def test_is_unique(data, expected):
# GH#11946 / GH#25180
ser = create_series_with_explicit_dtype(data, dtype_if_empty=object)
assert ser.is_unique is expected


def test_is_unique_class_ne(capsys):
# GH#20661
class Foo:
def __init__(self, val):
self._value = val

def __ne__(self, other):
raise Exception("NEQ not supported")

with capsys.disabled():
li = [Foo(i) for i in range(5)]
ser = Series(li, index=list(range(5)))

ser.is_unique
captured = capsys.readouterr()
assert len(captured.err) == 0
21 changes: 21 additions & 0 deletions pandas/tests/series/methods/test_nunique.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import numpy as np

from pandas import Categorical, Series


def test_nunique():
# basics.rst doc example
series = Series(np.random.randn(500))
series[20:500] = np.nan
series[10:20] = 5000
result = series.nunique()
assert result == 11


def test_nunique_categorical():
# GH#18051
ser = Series(Categorical([]))
assert ser.nunique() == 0

ser = Series(Categorical([np.nan]))
assert ser.nunique() == 0
49 changes: 49 additions & 0 deletions pandas/tests/series/methods/test_unique.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import numpy as np

from pandas import Categorical, Series
import pandas._testing as tm


class TestUnique:
def test_unique_data_ownership(self):
# it works! GH#1807
Series(Series(["a", "c", "b"]).unique()).sort_values()

def test_unique(self):
# GH#714 also, dtype=float
ser = Series([1.2345] * 100)
ser[::2] = np.nan
result = ser.unique()
assert len(result) == 2

# explicit f4 dtype
ser = Series([1.2345] * 100, dtype="f4")
ser[::2] = np.nan
result = ser.unique()
assert len(result) == 2

def test_unique_nan_object_dtype(self):
# NAs in object arrays GH#714
ser = Series(["foo"] * 100, dtype="O")
ser[::2] = np.nan
result = ser.unique()
assert len(result) == 2

def test_unique_none(self):
# decision about None
ser = Series([1, 2, 3, None, None, None], dtype=object)
result = ser.unique()
expected = np.array([1, 2, 3, None], dtype=object)
tm.assert_numpy_array_equal(result, expected)

def test_unique_categorical(self):
# GH#18051
cat = Categorical([])
ser = Series(cat)
result = ser.unique()
tm.assert_categorical_equal(result, cat)

cat = Categorical([np.nan])
ser = Series(cat)
result = ser.unique()
tm.assert_categorical_equal(result, cat)
10 changes: 0 additions & 10 deletions pandas/tests/series/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,6 @@


class TestSeriesMisc:
def test_getitem_preserve_name(self, datetime_series):
result = datetime_series[datetime_series > 0]
assert result.name == datetime_series.name

result = datetime_series[[0, 2, 4]]
assert result.name == datetime_series.name

result = datetime_series[5:10]
assert result.name == datetime_series.name

def test_tab_completion(self):
# GH 9910
s = Series(list("abcd"))
Expand Down
15 changes: 0 additions & 15 deletions pandas/tests/series/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,6 @@ def cmp(a, b):
result = ser.astype("object").astype(CategoricalDtype())
tm.assert_series_equal(result, roundtrip_expected)

def test_astype_categorical_invalid_conversions(self):
# invalid conversion (these are NOT a dtype)
cat = Categorical([f"{i} - {i + 499}" for i in range(0, 10000, 500)])
ser = Series(np.random.RandomState(0).randint(0, 10000, 100)).sort_values()
ser = pd.cut(ser, range(0, 10500, 500), right=False, labels=cat)

msg = (
"dtype '<class 'pandas.core.arrays.categorical.Categorical'>' "
"not understood"
)
with pytest.raises(TypeError, match=msg):
ser.astype(Categorical)
with pytest.raises(TypeError, match=msg):
ser.astype("object").astype(Categorical)

def test_series_to_categorical(self):
# see gh-16524: test conversion of Series to Categorical
series = Series(["a", "b", "c"])
Expand Down
Loading