Skip to content

CLN: misplaced TimedeltaIndex tests #31755

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 2 commits into from
Feb 9, 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: 9 additions & 1 deletion pandas/tests/frame/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ def test_loc_iterable(self, float_frame, key_type):
expected = float_frame.loc[:, ["A", "B", "C"]]
tm.assert_frame_equal(result, expected)

def test_loc_timedelta_0seconds(self):
# GH#10583
df = pd.DataFrame(np.random.normal(size=(10, 4)))
df.index = pd.timedelta_range(start="0s", periods=10, freq="s")
expected = df.loc[pd.Timedelta("0s") :, :]
result = df.loc["0s":, :]
tm.assert_frame_equal(expected, result)

@pytest.mark.parametrize(
"idx_type",
[
Expand Down Expand Up @@ -204,7 +212,7 @@ def test_setitem_list_of_tuples(self, float_frame):
expected = Series(tuples, index=float_frame.index, name="tuples")
tm.assert_series_equal(result, expected)

def test_setitem_mulit_index(self):
def test_setitem_multi_index(self):
# GH7655, test that assigning to a sub-frame of a frame
# with multi-index columns aligns both rows and columns
it = ["jim", "joe", "jolie"], ["first", "last"], ["left", "center", "right"]
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/indexes/timedeltas/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@


class TestTimedeltaIndex:
@pytest.mark.parametrize("unit", ["Y", "y", "M"])
def test_unit_m_y_raises(self, unit):
msg = "Units 'M' and 'Y' are no longer supported"
with pytest.raises(ValueError, match=msg):
TimedeltaIndex([1, 3, 7], unit)

def test_int64_nocopy(self):
# GH#23539 check that a copy isn't made when we pass int64 data
# and copy=False
Expand Down
12 changes: 0 additions & 12 deletions pandas/tests/indexes/timedeltas/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,6 @@ def test_order(self):
["1 day", "3 day", "5 day", "2 day", "1 day"], name="idx2"
)

# TODO(wesm): unused?
# exp2 = TimedeltaIndex(['1 day', '1 day', '2 day',
# '3 day', '5 day'], name='idx2')

# idx3 = TimedeltaIndex([pd.NaT, '3 minute', '5 minute',
# '2 minute', pd.NaT], name='idx3')
# exp3 = TimedeltaIndex([pd.NaT, pd.NaT, '2 minute', '3 minute',
# '5 minute'], name='idx3')

for idx, expected in [(idx1, exp1), (idx1, exp1), (idx1, exp1)]:
ordered = idx.sort_values()
tm.assert_index_equal(ordered, expected)
Expand Down Expand Up @@ -189,9 +180,6 @@ def test_infer_freq(self, freq):
tm.assert_index_equal(idx, result)
assert result.freq == freq

def test_shift(self):
pass # handled in test_arithmetic.py

def test_repeat(self):
index = pd.timedelta_range("1 days", periods=2, freq="D")
exp = pd.TimedeltaIndex(["1 days", "1 days", "2 days", "2 days"])
Expand Down
14 changes: 0 additions & 14 deletions pandas/tests/indexes/timedeltas/test_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,17 +284,3 @@ def test_freq_conversion(self):

result = td.astype("timedelta64[s]")
tm.assert_index_equal(result, expected)

@pytest.mark.parametrize("unit", ["Y", "y", "M"])
def test_unit_m_y_raises(self, unit):
msg = "Units 'M' and 'Y' are no longer supported"
with pytest.raises(ValueError, match=msg):
TimedeltaIndex([1, 3, 7], unit)


class TestTimeSeries:
def test_series_box_timedelta(self):
rng = timedelta_range("1 day 1 s", periods=5, freq="h")
s = Series(rng)
assert isinstance(s[1], Timedelta)
assert isinstance(s.iat[2], Timedelta)
18 changes: 0 additions & 18 deletions pandas/tests/indexes/timedeltas/test_timedelta_range.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import numpy as np
import pytest

import pandas as pd
from pandas import timedelta_range, to_timedelta
import pandas._testing as tm

Expand Down Expand Up @@ -31,23 +30,6 @@ def test_timedelta_range(self):
result = timedelta_range("0 days", freq="30T", periods=50)
tm.assert_index_equal(result, expected)

# GH 11776
arr = np.arange(10).reshape(2, 5)
df = pd.DataFrame(np.arange(10).reshape(2, 5))
for arg in (arr, df):
with pytest.raises(TypeError, match="1-d array"):
to_timedelta(arg)
for errors in ["ignore", "raise", "coerce"]:
with pytest.raises(TypeError, match="1-d array"):
to_timedelta(arg, errors=errors)

# issue10583
df = pd.DataFrame(np.random.normal(size=(10, 4)))
df.index = pd.timedelta_range(start="0s", periods=10, freq="s")
expected = df.loc[pd.Timedelta("0s") :, :]
result = df.loc["0s":, :]
tm.assert_frame_equal(expected, result)

@pytest.mark.parametrize(
"periods, freq", [(3, "2D"), (5, "D"), (6, "19H12T"), (7, "16H"), (9, "12H")]
)
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/indexes/timedeltas/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ def test_to_timedelta(self):
expected = TimedeltaIndex([np.timedelta64(1, "D")] * 5)
tm.assert_index_equal(result, expected)

def test_to_timedelta_dataframe(self):
# GH 11776
arr = np.arange(10).reshape(2, 5)
df = pd.DataFrame(np.arange(10).reshape(2, 5))
for arg in (arr, df):
with pytest.raises(TypeError, match="1-d array"):
to_timedelta(arg)
for errors in ["ignore", "raise", "coerce"]:
with pytest.raises(TypeError, match="1-d array"):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this actually have different behavor for coerce / ignore? e.g. this should not raise.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with a 2D input it always raises

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh ok, its to_timedelta, nvm

to_timedelta(arg, errors=errors)

def test_to_timedelta_invalid(self):

# bad value for errors parameter
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/series/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,16 @@ def test_series_box_timestamp():
assert isinstance(ser.iat[5], pd.Timestamp)


def test_series_box_timedelta():
rng = pd.timedelta_range("1 day 1 s", periods=5, freq="h")
ser = pd.Series(rng)
assert isinstance(ser[0], Timedelta)
assert isinstance(ser.at[1], Timedelta)
assert isinstance(ser.iat[2], Timedelta)
assert isinstance(ser.loc[3], Timedelta)
assert isinstance(ser.iloc[4], Timedelta)


def test_getitem_ambiguous_keyerror():
s = Series(range(10), index=list(range(0, 20, 2)))
with pytest.raises(KeyError, match=r"^1$"):
Expand Down