Skip to content

Commit

Permalink
REF: organize Series indexing tests (#31557)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel committed Feb 2, 2020
1 parent 84c01bc commit b310ed9
Show file tree
Hide file tree
Showing 12 changed files with 679 additions and 665 deletions.
494 changes: 1 addition & 493 deletions pandas/tests/series/indexing/test_boolean.py

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions pandas/tests/series/indexing/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ def test_frame_datetime64_duplicated():

def test_getitem_setitem_datetime_tz_pytz():
from pytz import timezone as tz
from pandas import date_range

N = 50
# testing with timezone, GH #2785
Expand Down Expand Up @@ -189,8 +188,6 @@ def test_getitem_setitem_datetime_tz_dateutil():
lambda x: tzutc() if x == "UTC" else gettz(x)
) # handle special case for utc in dateutil

from pandas import date_range

N = 50

# testing with timezone, GH #2785
Expand Down Expand Up @@ -373,7 +370,6 @@ def test_getitem_median_slice_bug():


def test_datetime_indexing():
from pandas import date_range

index = date_range("1/1/2000", "1/7/2000")
index = index.repeat(3)
Expand Down
134 changes: 134 additions & 0 deletions pandas/tests/series/indexing/test_get.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import numpy as np

import pandas as pd
from pandas import Series


def test_get():
# GH 6383
s = Series(
np.array(
[
43,
48,
60,
48,
50,
51,
50,
45,
57,
48,
56,
45,
51,
39,
55,
43,
54,
52,
51,
54,
]
)
)

result = s.get(25, 0)
expected = 0
assert result == expected

s = Series(
np.array(
[
43,
48,
60,
48,
50,
51,
50,
45,
57,
48,
56,
45,
51,
39,
55,
43,
54,
52,
51,
54,
]
),
index=pd.Float64Index(
[
25.0,
36.0,
49.0,
64.0,
81.0,
100.0,
121.0,
144.0,
169.0,
196.0,
1225.0,
1296.0,
1369.0,
1444.0,
1521.0,
1600.0,
1681.0,
1764.0,
1849.0,
1936.0,
]
),
)

result = s.get(25, 0)
expected = 43
assert result == expected

# GH 7407
# with a boolean accessor
df = pd.DataFrame({"i": [0] * 3, "b": [False] * 3})
vc = df.i.value_counts()
result = vc.get(99, default="Missing")
assert result == "Missing"

vc = df.b.value_counts()
result = vc.get(False, default="Missing")
assert result == 3

result = vc.get(True, default="Missing")
assert result == "Missing"


def test_get_nan():
# GH 8569
s = pd.Float64Index(range(10)).to_series()
assert s.get(np.nan) is None
assert s.get(np.nan, default="Missing") == "Missing"


def test_get_nan_multiple():
# GH 8569
# ensure that fixing "test_get_nan" above hasn't broken get
# with multiple elements
s = pd.Float64Index(range(10)).to_series()

idx = [2, 30]
assert s.get(idx) is None

idx = [2, np.nan]
assert s.get(idx) is None

# GH 17295 - all missing keys
idx = [20, 30]
assert s.get(idx) is None

idx = [np.nan, np.nan]
assert s.get(idx) is None
35 changes: 0 additions & 35 deletions pandas/tests/series/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -883,41 +883,6 @@ def test_pop():
tm.assert_series_equal(k, expected)


def test_take():
s = Series([-1, 5, 6, 2, 4])

actual = s.take([1, 3, 4])
expected = Series([5, 2, 4], index=[1, 3, 4])
tm.assert_series_equal(actual, expected)

actual = s.take([-1, 3, 4])
expected = Series([4, 2, 4], index=[4, 3, 4])
tm.assert_series_equal(actual, expected)

msg = "index {} is out of bounds for( axis 0 with)? size 5"
with pytest.raises(IndexError, match=msg.format(10)):
s.take([1, 10])
with pytest.raises(IndexError, match=msg.format(5)):
s.take([2, 5])


def test_take_categorical():
# https://github.com/pandas-dev/pandas/issues/20664
s = Series(pd.Categorical(["a", "b", "c"]))
result = s.take([-2, -2, 0])
expected = Series(
pd.Categorical(["b", "b", "a"], categories=["a", "b", "c"]), index=[1, 1, 0]
)
tm.assert_series_equal(result, expected)


def test_head_tail(string_series):
tm.assert_series_equal(string_series.head(), string_series[:5])
tm.assert_series_equal(string_series.head(0), string_series[0:0])
tm.assert_series_equal(string_series.tail(), string_series[-5:])
tm.assert_series_equal(string_series.tail(0), string_series[0:0])


def test_uint_drop(any_int_dtype):
# see GH18311
# assigning series.loc[0] = 4 changed series.dtype to int
Expand Down
65 changes: 65 additions & 0 deletions pandas/tests/series/indexing/test_mask.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import numpy as np
import pytest

from pandas import Series
import pandas._testing as tm


def test_mask():
# compare with tested results in test_where
s = Series(np.random.randn(5))
cond = s > 0

rs = s.where(~cond, np.nan)
tm.assert_series_equal(rs, s.mask(cond))

rs = s.where(~cond)
rs2 = s.mask(cond)
tm.assert_series_equal(rs, rs2)

rs = s.where(~cond, -s)
rs2 = s.mask(cond, -s)
tm.assert_series_equal(rs, rs2)

cond = Series([True, False, False, True, False], index=s.index)
s2 = -(s.abs())
rs = s2.where(~cond[:3])
rs2 = s2.mask(cond[:3])
tm.assert_series_equal(rs, rs2)

rs = s2.where(~cond[:3], -s2)
rs2 = s2.mask(cond[:3], -s2)
tm.assert_series_equal(rs, rs2)

msg = "Array conditional must be same shape as self"
with pytest.raises(ValueError, match=msg):
s.mask(1)
with pytest.raises(ValueError, match=msg):
s.mask(cond[:3].values, -s)

# dtype changes
s = Series([1, 2, 3, 4])
result = s.mask(s > 2, np.nan)
expected = Series([1, 2, np.nan, np.nan])
tm.assert_series_equal(result, expected)

# see gh-21891
s = Series([1, 2])
res = s.mask([True, False])

exp = Series([np.nan, 2])
tm.assert_series_equal(res, exp)


def test_mask_inplace():
s = Series(np.random.randn(5))
cond = s > 0

rs = s.copy()
rs.mask(cond, inplace=True)
tm.assert_series_equal(rs.dropna(), s[~cond])
tm.assert_series_equal(rs, s.mask(cond))

rs = s.copy()
rs.mask(cond, -s, inplace=True)
tm.assert_series_equal(rs, s.mask(cond, -s))
Loading

0 comments on commit b310ed9

Please sign in to comment.