Skip to content
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

TST/REF: share method tests between DataFrame and Series #37596

Merged
merged 8 commits into from
Nov 3, 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
10 changes: 10 additions & 0 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,16 @@ def unique_nulls_fixture(request):
# ----------------------------------------------------------------
# Classes
# ----------------------------------------------------------------


@pytest.fixture(params=[pd.DataFrame, pd.Series])
def frame_or_series(request):
"""
Fixture to parametrize over DataFrame and Series.
"""
return request.param


@pytest.fixture(
params=[pd.Index, pd.Series], ids=["index", "series"] # type: ignore[list-item]
)
Expand Down
14 changes: 13 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,13 @@
from pandas.core.construction import extract_array
from pandas.core.generic import NDFrame, _shared_docs
from pandas.core.indexes import base as ibase
from pandas.core.indexes.api import Index, ensure_index, ensure_index_from_sequences
from pandas.core.indexes.api import (
DatetimeIndex,
Index,
PeriodIndex,
ensure_index,
ensure_index_from_sequences,
)
from pandas.core.indexes.multi import MultiIndex, maybe_droplevels
from pandas.core.indexing import check_bool_indexer, convert_to_index_sliceable
from pandas.core.internals import BlockManager
Expand Down Expand Up @@ -9253,6 +9259,9 @@ def to_timestamp(

axis_name = self._get_axis_name(axis)
old_ax = getattr(self, axis_name)
if not isinstance(old_ax, PeriodIndex):
raise TypeError(f"unsupported Type {type(old_ax).__name__}")

new_ax = old_ax.to_timestamp(freq=freq, how=how)

setattr(new_obj, axis_name, new_ax)
Expand Down Expand Up @@ -9282,6 +9291,9 @@ def to_period(self, freq=None, axis: Axis = 0, copy: bool = True) -> DataFrame:

axis_name = self._get_axis_name(axis)
old_ax = getattr(self, axis_name)
if not isinstance(old_ax, DatetimeIndex):
raise TypeError(f"unsupported Type {type(old_ax).__name__}")

new_ax = old_ax.to_period(freq=freq)

setattr(new_obj, axis_name, new_ax)
Expand Down
45 changes: 33 additions & 12 deletions pandas/tests/frame/methods/test_at_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,57 @@
import pytest
import pytz

from pandas._libs.tslibs import timezones

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


class TestAtTime:
def test_at_time(self):
@pytest.mark.parametrize("tzstr", ["US/Eastern", "dateutil/US/Eastern"])
def test_localized_at_time(self, tzstr, frame_or_series):
tz = timezones.maybe_get_tz(tzstr)

rng = date_range("4/16/2012", "5/1/2012", freq="H")
ts = frame_or_series(np.random.randn(len(rng)), index=rng)

ts_local = ts.tz_localize(tzstr)

result = ts_local.at_time(time(10, 0))
expected = ts.at_time(time(10, 0)).tz_localize(tzstr)
tm.assert_equal(result, expected)
assert timezones.tz_compare(result.index.tz, tz)

def test_at_time(self, frame_or_series):
rng = date_range("1/1/2000", "1/5/2000", freq="5min")
ts = DataFrame(np.random.randn(len(rng), 2), index=rng)
if frame_or_series is not DataFrame:
ts = ts[0]
rs = ts.at_time(rng[1])
assert (rs.index.hour == rng[1].hour).all()
assert (rs.index.minute == rng[1].minute).all()
assert (rs.index.second == rng[1].second).all()

result = ts.at_time("9:30")
expected = ts.at_time(time(9, 30))
tm.assert_frame_equal(result, expected)

result = ts.loc[time(9, 30)]
expected = ts.loc[(rng.hour == 9) & (rng.minute == 30)]

tm.assert_frame_equal(result, expected)
tm.assert_equal(result, expected)

def test_at_time_midnight(self, frame_or_series):
# midnight, everything
rng = date_range("1/1/2000", "1/31/2000")
ts = DataFrame(np.random.randn(len(rng), 3), index=rng)
if frame_or_series is not DataFrame:
ts = ts[0]

result = ts.at_time(time(0, 0))
tm.assert_frame_equal(result, ts)
tm.assert_equal(result, ts)

def test_at_time_nonexistent(self, frame_or_series):
# time doesn't exist
rng = date_range("1/1/2012", freq="23Min", periods=384)
ts = DataFrame(np.random.randn(len(rng), 2), rng)
ts = DataFrame(np.random.randn(len(rng)), rng)
if frame_or_series is not DataFrame:
ts = ts[0]
rs = ts.at_time("16:00")
assert len(rs) == 0

Expand All @@ -62,12 +81,14 @@ def test_at_time_tz(self):
expected = df.iloc[1:2]
tm.assert_frame_equal(result, expected)

def test_at_time_raises(self):
def test_at_time_raises(self, frame_or_series):
# GH#20725
df = DataFrame([[1, 2, 3], [4, 5, 6]])
obj = DataFrame([[1, 2, 3], [4, 5, 6]])
if frame_or_series is not DataFrame:
obj = obj[0]
msg = "Index must be DatetimeIndex"
with pytest.raises(TypeError, match=msg): # index is not a DatetimeIndex
df.at_time("00:00")
obj.at_time("00:00")

@pytest.mark.parametrize("axis", ["index", "columns", 0, 1])
def test_at_time_axis(self, axis):
Expand Down
94 changes: 86 additions & 8 deletions pandas/tests/frame/methods/test_between_time.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,73 @@
from datetime import time
from datetime import datetime, time

import numpy as np
import pytest

from pandas import DataFrame, date_range
from pandas._libs.tslibs import timezones
import pandas.util._test_decorators as td

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


class TestBetweenTime:
def test_between_time(self, close_open_fixture):
@td.skip_if_has_locale
def test_between_time_formats(self, frame_or_series):
# GH#11818
rng = date_range("1/1/2000", "1/5/2000", freq="5min")
ts = DataFrame(np.random.randn(len(rng), 2), index=rng)
if frame_or_series is Series:
ts = ts[0]

strings = [
("2:00", "2:30"),
("0200", "0230"),
("2:00am", "2:30am"),
("0200am", "0230am"),
("2:00:00", "2:30:00"),
("020000", "023000"),
("2:00:00am", "2:30:00am"),
("020000am", "023000am"),
]
expected_length = 28

for time_string in strings:
assert len(ts.between_time(*time_string)) == expected_length

@pytest.mark.parametrize("tzstr", ["US/Eastern", "dateutil/US/Eastern"])
def test_localized_between_time(self, tzstr, frame_or_series):
tz = timezones.maybe_get_tz(tzstr)

rng = date_range("4/16/2012", "5/1/2012", freq="H")
ts = Series(np.random.randn(len(rng)), index=rng)
if frame_or_series is DataFrame:
ts = ts.to_frame()

ts_local = ts.tz_localize(tzstr)

t1, t2 = time(10, 0), time(11, 0)
result = ts_local.between_time(t1, t2)
expected = ts.between_time(t1, t2).tz_localize(tzstr)
tm.assert_equal(result, expected)
assert timezones.tz_compare(result.index.tz, tz)

def test_between_time_types(self, frame_or_series):
# GH11818
rng = date_range("1/1/2000", "1/5/2000", freq="5min")
obj = DataFrame({"A": 0}, index=rng)
if frame_or_series is Series:
obj = obj["A"]

msg = r"Cannot convert arg \[datetime\.datetime\(2010, 1, 2, 1, 0\)\] to a time"
with pytest.raises(ValueError, match=msg):
obj.between_time(datetime(2010, 1, 2, 1), datetime(2010, 1, 2, 5))

def test_between_time(self, close_open_fixture, frame_or_series):
rng = date_range("1/1/2000", "1/5/2000", freq="5min")
ts = DataFrame(np.random.randn(len(rng), 2), index=rng)
if frame_or_series is not DataFrame:
ts = ts[0]

stime = time(0, 0)
etime = time(1, 0)
inc_start, inc_end = close_open_fixture
Expand All @@ -37,11 +94,13 @@ def test_between_time(self, close_open_fixture):

result = ts.between_time("00:00", "01:00")
expected = ts.between_time(stime, etime)
tm.assert_frame_equal(result, expected)
tm.assert_equal(result, expected)

# across midnight
rng = date_range("1/1/2000", "1/5/2000", freq="5min")
ts = DataFrame(np.random.randn(len(rng), 2), index=rng)
if frame_or_series is not DataFrame:
ts = ts[0]
stime = time(22, 0)
etime = time(9, 0)

Expand All @@ -65,14 +124,33 @@ def test_between_time(self, close_open_fixture):
else:
assert (t < etime) or (t >= stime)

def test_between_time_raises(self):
def test_between_time_raises(self, frame_or_series):
# GH#20725
df = DataFrame([[1, 2, 3], [4, 5, 6]])
obj = DataFrame([[1, 2, 3], [4, 5, 6]])
if frame_or_series is not DataFrame:
obj = obj[0]

msg = "Index must be DatetimeIndex"
with pytest.raises(TypeError, match=msg): # index is not a DatetimeIndex
df.between_time(start_time="00:00", end_time="12:00")
obj.between_time(start_time="00:00", end_time="12:00")

def test_between_time_axis(self, frame_or_series):
# GH#8839
rng = date_range("1/1/2000", periods=100, freq="10min")
ts = Series(np.random.randn(len(rng)), index=rng)
if frame_or_series is DataFrame:
ts = ts.to_frame()

stime, etime = ("08:00:00", "09:00:00")
expected_length = 7

assert len(ts.between_time(stime, etime)) == expected_length
assert len(ts.between_time(stime, etime, axis=0)) == expected_length
msg = f"No axis named {ts.ndim} for object type {type(ts).__name__}"
with pytest.raises(ValueError, match=msg):
ts.between_time(stime, etime, axis=ts.ndim)

def test_between_time_axis(self, axis):
def test_between_time_axis_aliases(self, axis):
# GH#8839
rng = date_range("1/1/2000", periods=100, freq="10min")
ts = DataFrame(np.random.randn(len(rng), len(rng)))
Expand Down
77 changes: 64 additions & 13 deletions pandas/tests/frame/methods/test_to_period.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,87 @@
import numpy as np
import pytest

from pandas import DataFrame, date_range, period_range
from pandas import (
DataFrame,
DatetimeIndex,
PeriodIndex,
Series,
date_range,
period_range,
)
import pandas._testing as tm


class TestToPeriod:
def test_frame_to_period(self):
def test_to_period(self, frame_or_series):
K = 5

dr = date_range("1/1/2000", "1/1/2001")
pr = period_range("1/1/2000", "1/1/2001")
df = DataFrame(np.random.randn(len(dr), K), index=dr)
df["mix"] = "a"
dr = date_range("1/1/2000", "1/1/2001", freq="D")
obj = DataFrame(
np.random.randn(len(dr), K), index=dr, columns=["A", "B", "C", "D", "E"]
)
obj["mix"] = "a"
if frame_or_series is Series:
obj = obj["A"]

pts = df.to_period()
exp = df.copy()
exp.index = pr
tm.assert_frame_equal(pts, exp)
pts = obj.to_period()
exp = obj.copy()
exp.index = period_range("1/1/2000", "1/1/2001")
tm.assert_equal(pts, exp)

pts = df.to_period("M")
tm.assert_index_equal(pts.index, exp.index.asfreq("M"))
pts = obj.to_period("M")
exp.index = exp.index.asfreq("M")
tm.assert_equal(pts, exp)

def test_to_period_without_freq(self, frame_or_series):
# GH#7606 without freq
idx = DatetimeIndex(["2011-01-01", "2011-01-02", "2011-01-03", "2011-01-04"])
exp_idx = PeriodIndex(
["2011-01-01", "2011-01-02", "2011-01-03", "2011-01-04"], freq="D"
)

obj = DataFrame(np.random.randn(4, 4), index=idx, columns=idx)
if frame_or_series is Series:
obj = obj[idx[0]]
expected = obj.copy()
expected.index = exp_idx
tm.assert_equal(obj.to_period(), expected)

if frame_or_series is DataFrame:
expected = obj.copy()
expected.columns = exp_idx
tm.assert_frame_equal(obj.to_period(axis=1), expected)

def test_to_period_columns(self):
dr = date_range("1/1/2000", "1/1/2001")
df = DataFrame(np.random.randn(len(dr), 5), index=dr)
df["mix"] = "a"

df = df.T
pts = df.to_period(axis=1)
exp = df.copy()
exp.columns = pr
exp.columns = period_range("1/1/2000", "1/1/2001")
tm.assert_frame_equal(pts, exp)

pts = df.to_period("M", axis=1)
tm.assert_index_equal(pts.columns, exp.columns.asfreq("M"))

def test_to_period_invalid_axis(self):
dr = date_range("1/1/2000", "1/1/2001")
df = DataFrame(np.random.randn(len(dr), 5), index=dr)
df["mix"] = "a"

msg = "No axis named 2 for object type DataFrame"
with pytest.raises(ValueError, match=msg):
df.to_period(axis=2)

def test_to_period_raises(self, index, frame_or_series):
# https://github.com/pandas-dev/pandas/issues/33327
obj = Series(index=index, dtype=object)
if frame_or_series is DataFrame:
obj = obj.to_frame()

if not isinstance(index, DatetimeIndex):
msg = f"unsupported Type {type(index).__name__}"
with pytest.raises(TypeError, match=msg):
obj.to_period()
Loading