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: collect tests by method #37617

Merged
merged 5 commits into from
Nov 4, 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
11 changes: 11 additions & 0 deletions pandas/tests/frame/methods/test_asfreq.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,14 @@ def test_asfreq_fillvalue(self):
expected_series = ts.asfreq(freq="1S").fillna(9.0)
actual_series = ts.asfreq(freq="1S", fill_value=9.0)
tm.assert_series_equal(expected_series, actual_series)

def test_asfreq_with_date_object_index(self, frame_or_series):
rng = date_range("1/1/2000", periods=20)
ts = frame_or_series(np.random.randn(20), index=rng)

ts2 = ts.copy()
ts2.index = [x.date() for x in ts2.index]

result = ts2.asfreq("4H", method="ffill")
expected = ts.asfreq("4H", method="ffill")
tm.assert_equal(result, expected)
19 changes: 18 additions & 1 deletion pandas/tests/frame/methods/test_values.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np
import pytest

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


Expand Down Expand Up @@ -44,6 +45,22 @@ def test_values_duplicates(self):

tm.assert_numpy_array_equal(result, expected)

@pytest.mark.parametrize("constructor", [date_range, period_range])
def test_values_casts_datetimelike_to_object(self, constructor):
series = Series(constructor("2000-01-01", periods=10, freq="D"))

expected = series.astype("object")

df = DataFrame({"a": series, "b": np.random.randn(len(series))})

result = df.values.squeeze()
assert (result[:, 0] == expected.values).all()

df = DataFrame({"a": series, "b": ["foo"] * len(series)})

result = df.values.squeeze()
assert (result[:, 0] == expected.values).all()

def test_frame_values_with_tz(self):
tz = "US/Central"
df = DataFrame({"A": date_range("2000", periods=4, tz=tz)})
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/indexes/datetimes/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,13 @@ def test_contains_nonunique(self, vals):


class TestGetIndexer:
def test_get_indexer_date_objs(self):
rng = date_range("1/1/2000", periods=20)

result = rng.get_indexer(rng.map(lambda x: x.date()))
expected = rng.get_indexer(rng)
tm.assert_numpy_array_equal(result, expected)

def test_get_indexer(self):
idx = pd.date_range("2000-01-01", periods=3)
exp = np.array([0, 1, 2], dtype=np.intp)
Expand Down
13 changes: 12 additions & 1 deletion pandas/tests/series/apply/test_series_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,23 @@
import pytest

import pandas as pd
from pandas import DataFrame, Index, MultiIndex, Series, isna
from pandas import DataFrame, Index, MultiIndex, Series, isna, timedelta_range
import pandas._testing as tm
from pandas.core.base import SpecificationError


class TestSeriesApply:
def test_series_map_box_timedelta(self):
# GH#11349
ser = Series(timedelta_range("1 day 1 s", periods=5, freq="h"))

def f(x):
return x.total_seconds()

ser.map(f)
ser.apply(f)
DataFrame(ser).applymap(f)

def test_apply(self, datetime_series):
with np.errstate(all="ignore"):
tm.assert_series_equal(
Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/series/methods/test_values.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import numpy as np
import pytest

from pandas import IntervalIndex, Series, period_range
import pandas._testing as tm


class TestValues:
@pytest.mark.parametrize(
"data",
[
period_range("2000", periods=4),
IntervalIndex.from_breaks([1, 2, 3, 4]),
],
)
def test_values_object_extension_dtypes(self, data):
# https://github.com/pandas-dev/pandas/issues/23995
result = Series(data).values
expected = np.array(data.astype(object))
tm.assert_numpy_array_equal(result, expected)
15 changes: 15 additions & 0 deletions pandas/tests/series/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,21 @@ def test_datetime_understood(self):
expected = Series(pd.to_datetime(["2011-12-26", "2011-12-27", "2011-12-28"]))
tm.assert_series_equal(result, expected)

def test_align_date_objects_with_datetimeindex(self):
rng = date_range("1/1/2000", periods=20)
ts = Series(np.random.randn(20), index=rng)

ts_slice = ts[5:]
ts2 = ts_slice.copy()
ts2.index = [x.date() for x in ts2.index]

result = ts + ts2
result2 = ts2 + ts
expected = ts + ts[5:]
expected.index = expected.index._with_freq(None)
tm.assert_series_equal(result, expected)
tm.assert_series_equal(result2, expected)


@pytest.mark.parametrize(
"names",
Expand Down
52 changes: 10 additions & 42 deletions pandas/tests/series/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pandas.core.dtypes.dtypes import CategoricalDtype

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


Expand Down Expand Up @@ -120,18 +120,20 @@ def cmp(a, b):
s.astype("object").astype(CategoricalDtype()), roundtrip_expected
)

def test_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.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"
)

for invalid in [
lambda x: x.astype(Categorical),
lambda x: x.astype("object").astype(Categorical),
]:
with pytest.raises(TypeError, match=msg):
invalid(s)
with pytest.raises(TypeError, match=msg):
ser.astype(Categorical)
with pytest.raises(TypeError, match=msg):
ser.astype("object").astype(Categorical)

@pytest.mark.parametrize("dtype", np.typecodes["All"])
def test_astype_empty_constructor_equality(self, dtype):
Expand All @@ -148,27 +150,6 @@ def test_astype_empty_constructor_equality(self, dtype):
as_type_empty = Series([]).astype(dtype)
tm.assert_series_equal(init_empty, as_type_empty)

def test_intercept_astype_object(self):
series = Series(date_range("1/1/2000", periods=10))

# This test no longer makes sense, as
# Series is by default already M8[ns].
expected = series.astype("object")

df = DataFrame({"a": series, "b": np.random.randn(len(series))})
exp_dtypes = Series(
[np.dtype("datetime64[ns]"), np.dtype("float64")], index=["a", "b"]
)
tm.assert_series_equal(df.dtypes, exp_dtypes)

result = df.values.squeeze()
assert (result[:, 0] == expected.values).all()

df = DataFrame({"a": series, "b": ["foo"] * len(series)})

result = df.values.squeeze()
assert (result[:, 0] == expected.values).all()

def test_series_to_categorical(self):
# see gh-16524: test conversion of Series to Categorical
series = Series(["a", "b", "c"])
Expand All @@ -178,19 +159,6 @@ def test_series_to_categorical(self):

tm.assert_series_equal(result, expected)

@pytest.mark.parametrize(
"data",
[
pd.period_range("2000", periods=4),
pd.IntervalIndex.from_breaks([1, 2, 3, 4]),
],
)
def test_values_compatibility(self, data):
# https://github.com/pandas-dev/pandas/issues/23995
result = Series(data).values
expected = np.array(data.astype(object))
tm.assert_numpy_array_equal(result, expected)

def test_reindex_astype_order_consistency(self):
# GH 17444
s = Series([1, 2, 3], index=[2, 0, 1])
Expand Down
24 changes: 0 additions & 24 deletions pandas/tests/series/test_period.py

This file was deleted.

41 changes: 0 additions & 41 deletions pandas/tests/series/test_timeseries.py

This file was deleted.