Skip to content

TST: implement test_getitem for Series #33353

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 14 commits into from
Apr 7, 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
14 changes: 13 additions & 1 deletion pandas/tests/frame/methods/test_duplicated.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np
import pytest

from pandas import DataFrame, Series
from pandas import DataFrame, Series, date_range
import pandas._testing as tm


Expand Down Expand Up @@ -95,3 +95,15 @@ def test_duplicated_on_empty_frame():
result = df[dupes]
expected = df.copy()
tm.assert_frame_equal(result, expected)


def test_frame_datetime64_duplicated():
dates = date_range("2010-07-01", end="2010-08-05")

tst = DataFrame({"symbol": "AAA", "date": dates})
result = tst.duplicated(["date", "symbol"])
assert (-result).all()

tst = DataFrame({"date": dates})
result = tst.duplicated()
assert (-result).all()
26 changes: 0 additions & 26 deletions pandas/tests/series/indexing/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,6 @@ def test_slicing_datetimes():
tm.assert_frame_equal(result, expected)


def test_frame_datetime64_duplicated():
dates = date_range("2010-07-01", end="2010-08-05")

tst = DataFrame({"symbol": "AAA", "date": dates})
result = tst.duplicated(["date", "symbol"])
assert (-result).all()

tst = DataFrame({"date": dates})
result = tst.duplicated()
assert (-result).all()


def test_getitem_setitem_datetime_tz_pytz():
from pytz import timezone as tz

Expand Down Expand Up @@ -353,20 +341,6 @@ def test_getitem_setitem_periodindex():
tm.assert_series_equal(result, ts)


# FutureWarning from NumPy.
@pytest.mark.filterwarnings("ignore:Using a non-tuple:FutureWarning")
def test_getitem_median_slice_bug():
index = date_range("20090415", "20090519", freq="2B")
s = Series(np.random.randn(13), index=index)

indexer = [slice(6, 7, None)]
with tm.assert_produces_warning(FutureWarning):
# GH#31299
result = s[indexer]
expected = s[indexer[0]]
tm.assert_series_equal(result, expected)


def test_datetime_indexing():

index = date_range("1/1/2000", "1/7/2000")
Expand Down
100 changes: 100 additions & 0 deletions pandas/tests/series/indexing/test_getitem.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
"""
Series.__getitem__ test classes are organized by the type of key passed.
"""
from datetime import datetime

import numpy as np
import pytest

from pandas._libs.tslibs import conversion, timezones

import pandas as pd
from pandas import Series, Timestamp, date_range, period_range
import pandas._testing as tm


class TestSeriesGetitemScalars:

# TODO: better name/GH ref?
def test_getitem_regression(self):
ser = Series(range(5), index=list(range(5)))
result = ser[list(range(5))]
tm.assert_series_equal(result, ser)

# ------------------------------------------------------------------
# Series with DatetimeIndex

@pytest.mark.parametrize("tzstr", ["Europe/Berlin", "dateutil/Europe/Berlin"])
def test_getitem_pydatetime_tz(self, tzstr):
tz = timezones.maybe_get_tz(tzstr)

index = date_range(
start="2012-12-24 16:00", end="2012-12-24 18:00", freq="H", tz=tzstr
)
ts = Series(index=index, data=index.hour)
time_pandas = Timestamp("2012-12-24 17:00", tz=tzstr)

dt = datetime(2012, 12, 24, 17, 0)
time_datetime = conversion.localize_pydatetime(dt, tz)
assert ts[time_pandas] == ts[time_datetime]

@pytest.mark.parametrize("tz", ["US/Eastern", "dateutil/US/Eastern"])
def test_string_index_alias_tz_aware(self, tz):
rng = date_range("1/1/2000", periods=10, tz=tz)
ser = Series(np.random.randn(len(rng)), index=rng)

result = ser["1/3/2000"]
tm.assert_almost_equal(result, ser[2])


class TestSeriesGetitemSlices:
def test_getitem_slice_2d(self, datetime_series):
# GH#30588 multi-dimensional indexing deprecated

# This is currently failing because the test was relying on
# the DeprecationWarning coming through Index.__getitem__.
# We want to implement a warning specifically for Series.__getitem__
# at which point this will become a Deprecation/FutureWarning
with tm.assert_produces_warning(None):
# GH#30867 Don't want to support this long-term, but
# for now ensure that the warning from Index
# doesn't comes through via Series.__getitem__.
result = datetime_series[:, np.newaxis]
expected = datetime_series.values[:, np.newaxis]
tm.assert_almost_equal(result, expected)

# FutureWarning from NumPy.
@pytest.mark.filterwarnings("ignore:Using a non-tuple:FutureWarning")
def test_getitem_median_slice_bug(self):
index = date_range("20090415", "20090519", freq="2B")
s = Series(np.random.randn(13), index=index)

indexer = [slice(6, 7, None)]
with tm.assert_produces_warning(FutureWarning):
# GH#31299
result = s[indexer]
expected = s[indexer[0]]
tm.assert_series_equal(result, expected)


class TestSeriesGetitemListLike:
def test_getitem_intlist_intindex_periodvalues(self):
ser = Series(period_range("2000-01-01", periods=10, freq="D"))

result = ser[[2, 4]]
exp = pd.Series(
[pd.Period("2000-01-03", freq="D"), pd.Period("2000-01-05", freq="D")],
index=[2, 4],
dtype="Period[D]",
)
tm.assert_series_equal(result, exp)
assert result.dtype == "Period[D]"


def test_getitem_generator(string_series):
gen = (x > 0 for x in string_series)
result = string_series[gen]
result2 = string_series[iter(string_series > 0)]
expected = string_series[string_series > 0]
tm.assert_series_equal(result, expected)
tm.assert_series_equal(result2, expected)
19 changes: 0 additions & 19 deletions pandas/tests/series/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,6 @@ def test_getitem_fancy(string_series, object_series):
assert object_series[2] == slice2[1]


def test_getitem_generator(string_series):
gen = (x > 0 for x in string_series)
result = string_series[gen]
result2 = string_series[iter(string_series > 0)]
expected = string_series[string_series > 0]
tm.assert_series_equal(result, expected)
tm.assert_series_equal(result2, expected)


def test_type_promotion():
# GH12599
s = pd.Series(dtype=object)
Expand Down Expand Up @@ -872,16 +863,6 @@ def test_uint_drop(any_int_dtype):
tm.assert_series_equal(series, expected)


def test_getitem_2d_no_warning():
# https://github.com/pandas-dev/pandas/issues/30867
# Don't want to support this long-term, but
# for now ensure that the warning from Index
# doesn't comes through via Series.__getitem__.
series = pd.Series([1, 2, 3], index=[1, 2, 3])
with tm.assert_produces_warning(None):
series[:, None]


def test_getitem_unrecognized_scalar():
# GH#32684 a scalar key that is not recognized by lib.is_scalar

Expand Down
6 changes: 0 additions & 6 deletions pandas/tests/series/indexing/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ def test_getitem_negative_out_of_bounds():
s[-11] = "foo"


def test_getitem_regression():
s = Series(range(5), index=list(range(5)))
result = s[list(range(5))]
tm.assert_series_equal(result, s)


def test_getitem_setitem_slice_bug():
s = Series(range(10), index=list(range(10)))
result = s[-12:]
Expand Down
12 changes: 0 additions & 12 deletions pandas/tests/series/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,6 @@ def test_auto_conversion(self):
)
assert series.dtype == "Period[D]"

def test_getitem(self):
assert self.series[1] == pd.Period("2000-01-02", freq="D")

result = self.series[[2, 4]]
exp = pd.Series(
[pd.Period("2000-01-03", freq="D"), pd.Period("2000-01-05", freq="D")],
index=[2, 4],
dtype="Period[D]",
)
tm.assert_series_equal(result, exp)
assert result.dtype == "Period[D]"

def test_isna(self):
# GH 13737
s = Series([pd.Period("2011-01", freq="M"), pd.Period("NaT", freq="M")])
Expand Down
12 changes: 0 additions & 12 deletions pandas/tests/series/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,6 @@


class TestTimeSeries:
def test_mpl_compat_hack(self, datetime_series):

# This is currently failing because the test was relying on
# the DeprecationWarning coming through Index.__getitem__.
# We want to implement a warning specifically for Series.__getitem__
# at which point this will become a Deprecation/FutureWarning
with tm.assert_produces_warning(None):
# GH#30588 multi-dimensional indexing deprecated
result = datetime_series[:, np.newaxis]
expected = datetime_series.values[:, np.newaxis]
tm.assert_almost_equal(result, expected)

def test_timeseries_coercion(self):
idx = tm.makeDateIndex(10000)
ser = Series(np.random.randn(len(idx)), idx.astype(object))
Expand Down
26 changes: 1 addition & 25 deletions pandas/tests/series/test_timezones.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
import numpy as np
import pytest

from pandas._libs.tslibs import conversion, timezones

from pandas import Series, Timestamp
from pandas import Series
import pandas._testing as tm
from pandas.core.indexes.datetimes import date_range

Expand All @@ -29,28 +27,6 @@ def test_dateutil_tzoffset_support(self):
# it works! #2443
repr(series.index[0])

@pytest.mark.parametrize("tz", ["US/Eastern", "dateutil/US/Eastern"])
def test_string_index_alias_tz_aware(self, tz):
rng = date_range("1/1/2000", periods=10, tz=tz)
ser = Series(np.random.randn(len(rng)), index=rng)

result = ser["1/3/2000"]
tm.assert_almost_equal(result, ser[2])

@pytest.mark.parametrize("tzstr", ["Europe/Berlin", "dateutil/Europe/Berlin"])
def test_getitem_pydatetime_tz(self, tzstr):
tz = timezones.maybe_get_tz(tzstr)

index = date_range(
start="2012-12-24 16:00", end="2012-12-24 18:00", freq="H", tz=tzstr
)
ts = Series(index=index, data=index.hour)
time_pandas = Timestamp("2012-12-24 17:00", tz=tzstr)

dt = datetime(2012, 12, 24, 17, 0)
time_datetime = conversion.localize_pydatetime(dt, tz)
assert ts[time_pandas] == ts[time_datetime]

@pytest.mark.parametrize("copy", [True, False])
@pytest.mark.parametrize(
"method, tz", [["tz_localize", None], ["tz_convert", "Europe/Berlin"]]
Expand Down