Skip to content

Commit

Permalink
Fixed test util imports in pandas/tests/indexes (#29289)
Browse files Browse the repository at this point in the history
xref: #29272
  • Loading branch information
SaturnFromTitan authored and gfyoung committed Oct 31, 2019
1 parent a49a587 commit b5c64dc
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 74 deletions.
3 changes: 1 addition & 2 deletions pandas/tests/indexes/datetimes/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import pandas as pd
from pandas import DataFrame, DatetimeIndex, Index, Timestamp, date_range, offsets
import pandas.util.testing as tm
from pandas.util.testing import assert_almost_equal

randn = np.random.randn

Expand Down Expand Up @@ -262,7 +261,7 @@ def test_isin(self):
result = index.isin(list(index))
assert result.all()

assert_almost_equal(
tm.assert_almost_equal(
index.isin([index[2], 5]), np.array([False, False, True, False])
)

Expand Down
55 changes: 27 additions & 28 deletions pandas/tests/indexes/datetimes/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
)
from pandas.core.arrays import DatetimeArray
from pandas.core.tools import datetimes as tools
from pandas.util import testing as tm
from pandas.util.testing import assert_series_equal
import pandas.util.testing as tm


class TestTimeConversionFormats:
Expand All @@ -55,7 +54,7 @@ def test_to_datetime_format(self, cache):
expected = expecteds[i]

if isinstance(expected, Series):
assert_series_equal(result, Series(expected))
tm.assert_series_equal(result, Series(expected))
elif isinstance(expected, Timestamp):
assert result == expected
else:
Expand All @@ -67,10 +66,10 @@ def test_to_datetime_format_YYYYMMDD(self, cache):
expected = Series([Timestamp(x) for x in s.apply(str)])

result = to_datetime(s, format="%Y%m%d", cache=cache)
assert_series_equal(result, expected)
tm.assert_series_equal(result, expected)

result = to_datetime(s.apply(str), format="%Y%m%d", cache=cache)
assert_series_equal(result, expected)
tm.assert_series_equal(result, expected)

# with NaT
expected = Series(
Expand All @@ -80,13 +79,13 @@ def test_to_datetime_format_YYYYMMDD(self, cache):
s[2] = np.nan

result = to_datetime(s, format="%Y%m%d", cache=cache)
assert_series_equal(result, expected)
tm.assert_series_equal(result, expected)

# string with NaT
s = s.apply(str)
s[2] = "nat"
result = to_datetime(s, format="%Y%m%d", cache=cache)
assert_series_equal(result, expected)
tm.assert_series_equal(result, expected)

# coercion
# GH 7930
Expand Down Expand Up @@ -131,7 +130,7 @@ def test_to_datetime_format_YYYYMMDD_overflow(self, input_s, expected):
# GH 25512
# format='%Y%m%d', errors='coerce'
result = pd.to_datetime(input_s, format="%Y%m%d", errors="coerce")
assert_series_equal(result, expected)
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize("cache", [True, False])
def test_to_datetime_format_integer(self, cache):
Expand All @@ -140,13 +139,13 @@ def test_to_datetime_format_integer(self, cache):
expected = Series([Timestamp(x) for x in s.apply(str)])

result = to_datetime(s, format="%Y", cache=cache)
assert_series_equal(result, expected)
tm.assert_series_equal(result, expected)

s = Series([200001, 200105, 200206])
expected = Series([Timestamp(x[:4] + "-" + x[4:]) for x in s.apply(str)])

result = to_datetime(s, format="%Y%m", cache=cache)
assert_series_equal(result, expected)
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize(
"int_date, expected",
Expand Down Expand Up @@ -216,7 +215,7 @@ def test_to_datetime_with_non_exact(self, cache):
expected = to_datetime(
s.str.extract(r"(\d+\w+\d+)", expand=False), format="%d%b%y", cache=cache
)
assert_series_equal(result, expected)
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize("cache", [True, False])
def test_parse_nanoseconds_with_formula(self, cache):
Expand Down Expand Up @@ -1204,11 +1203,11 @@ def test_dataframe(self, cache):
expected = Series(
[Timestamp("20150204 00:00:00"), Timestamp("20160305 00:0:00")]
)
assert_series_equal(result, expected)
tm.assert_series_equal(result, expected)

# dict-like
result = to_datetime(df[["year", "month", "day"]].to_dict(), cache=cache)
assert_series_equal(result, expected)
tm.assert_series_equal(result, expected)

# dict but with constructable
df2 = df[["year", "month", "day"]].to_dict()
Expand All @@ -1217,7 +1216,7 @@ def test_dataframe(self, cache):
expected2 = Series(
[Timestamp("20150204 00:00:00"), Timestamp("20160205 00:0:00")]
)
assert_series_equal(result, expected2)
tm.assert_series_equal(result, expected2)

# unit mappings
units = [
Expand All @@ -1244,7 +1243,7 @@ def test_dataframe(self, cache):
expected = Series(
[Timestamp("20150204 06:58:10"), Timestamp("20160305 07:59:11")]
)
assert_series_equal(result, expected)
tm.assert_series_equal(result, expected)

d = {
"year": "year",
Expand All @@ -1265,11 +1264,11 @@ def test_dataframe(self, cache):
Timestamp("20160305 07:59:11.001002003"),
]
)
assert_series_equal(result, expected)
tm.assert_series_equal(result, expected)

# coerce back to int
result = to_datetime(df.astype(str), cache=cache)
assert_series_equal(result, expected)
tm.assert_series_equal(result, expected)

# passing coerce
df2 = DataFrame({"year": [2015, 2016], "month": [2, 20], "day": [4, 5]})
Expand All @@ -1282,7 +1281,7 @@ def test_dataframe(self, cache):
to_datetime(df2, cache=cache)
result = to_datetime(df2, errors="coerce", cache=cache)
expected = Series([Timestamp("20150204 00:00:00"), NaT])
assert_series_equal(result, expected)
tm.assert_series_equal(result, expected)

# extra columns
msg = "extra keys have been passed to the datetime assemblage: " r"\[foo\]"
Expand Down Expand Up @@ -1330,7 +1329,7 @@ def test_dataframe_dtypes(self, cache):
expected = Series(
[Timestamp("20150204 00:00:00"), Timestamp("20160305 00:00:00")]
)
assert_series_equal(result, expected)
tm.assert_series_equal(result, expected)

# mixed dtypes
df["month"] = df["month"].astype("int8")
Expand All @@ -1339,7 +1338,7 @@ def test_dataframe_dtypes(self, cache):
expected = Series(
[Timestamp("20150204 00:00:00"), Timestamp("20160305 00:00:00")]
)
assert_series_equal(result, expected)
tm.assert_series_equal(result, expected)

# float
df = DataFrame({"year": [2000, 2001], "month": [1.5, 1], "day": [1, 1]})
Expand Down Expand Up @@ -1434,7 +1433,7 @@ def test_to_datetime_with_apply(self, cache):
td = Series(["May 04", "Jun 02", "Dec 11"], index=[1, 2, 3])
expected = pd.to_datetime(td, format="%b %y", cache=cache)
result = td.apply(pd.to_datetime, format="%b %y", cache=cache)
assert_series_equal(result, expected)
tm.assert_series_equal(result, expected)

td = pd.Series(["May 04", "Jun 02", ""], index=[1, 2, 3])
msg = r"time data '' does not match format '%b %y' \(match\)"
Expand All @@ -1447,7 +1446,7 @@ def test_to_datetime_with_apply(self, cache):
result = td.apply(
lambda x: pd.to_datetime(x, format="%b %y", errors="coerce", cache=cache)
)
assert_series_equal(result, expected)
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize("cache", [True, False])
def test_to_datetime_types(self, cache):
Expand Down Expand Up @@ -1584,10 +1583,10 @@ def test_string_na_nat_conversion(self, cache):
else:
expected[i] = to_datetime(x, cache=cache)

assert_series_equal(result, expected, check_names=False)
tm.assert_series_equal(result, expected, check_names=False)
assert result.name == "foo"

assert_series_equal(dresult, expected, check_names=False)
tm.assert_series_equal(dresult, expected, check_names=False)
assert dresult.name == "foo"

@pytest.mark.parametrize(
Expand Down Expand Up @@ -2158,20 +2157,20 @@ def test_to_basic(self, julian_dates):
expected = Series(
pd.to_datetime(julian_dates - pd.Timestamp(0).to_julian_date(), unit="D")
)
assert_series_equal(result, expected)
tm.assert_series_equal(result, expected)

result = Series(pd.to_datetime([0, 1, 2], unit="D", origin="unix"))
expected = Series(
[Timestamp("1970-01-01"), Timestamp("1970-01-02"), Timestamp("1970-01-03")]
)
assert_series_equal(result, expected)
tm.assert_series_equal(result, expected)

# default
result = Series(pd.to_datetime([0, 1, 2], unit="D"))
expected = Series(
[Timestamp("1970-01-01"), Timestamp("1970-01-02"), Timestamp("1970-01-03")]
)
assert_series_equal(result, expected)
tm.assert_series_equal(result, expected)

def test_julian_round_trip(self):
result = pd.to_datetime(2456658, origin="julian", unit="D")
Expand Down Expand Up @@ -2204,7 +2203,7 @@ def test_epoch(self, units, epochs, epoch_1960, units_from_epochs):
)

result = Series(pd.to_datetime(units_from_epochs, unit=units, origin=epochs))
assert_series_equal(result, expected)
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize(
"origin, exc",
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/indexes/multi/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

from pandas.core.dtypes.dtypes import CategoricalDtype

from pandas.util.testing import assert_copy
import pandas.util.testing as tm


def test_astype(idx):
expected = idx.copy()
actual = idx.astype("O")
assert_copy(actual.levels, expected.levels)
assert_copy(actual.codes, expected.codes)
tm.assert_copy(actual.levels, expected.levels)
tm.assert_copy(actual.codes, expected.codes)
assert actual.names == list(expected.names)

with pytest.raises(TypeError, match="^Setting.*dtype.*object"):
Expand Down
17 changes: 8 additions & 9 deletions pandas/tests/indexes/multi/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
)
from pandas.core.indexes.base import InvalidIndexError
import pandas.util.testing as tm
from pandas.util.testing import assert_almost_equal


def test_slice_locs_partial(idx):
Expand Down Expand Up @@ -145,32 +144,32 @@ def test_get_indexer():
idx2 = index[[1, 3, 5]]

r1 = idx1.get_indexer(idx2)
assert_almost_equal(r1, np.array([1, 3, -1], dtype=np.intp))
tm.assert_almost_equal(r1, np.array([1, 3, -1], dtype=np.intp))

r1 = idx2.get_indexer(idx1, method="pad")
e1 = np.array([-1, 0, 0, 1, 1], dtype=np.intp)
assert_almost_equal(r1, e1)
tm.assert_almost_equal(r1, e1)

r2 = idx2.get_indexer(idx1[::-1], method="pad")
assert_almost_equal(r2, e1[::-1])
tm.assert_almost_equal(r2, e1[::-1])

rffill1 = idx2.get_indexer(idx1, method="ffill")
assert_almost_equal(r1, rffill1)
tm.assert_almost_equal(r1, rffill1)

r1 = idx2.get_indexer(idx1, method="backfill")
e1 = np.array([0, 0, 1, 1, 2], dtype=np.intp)
assert_almost_equal(r1, e1)
tm.assert_almost_equal(r1, e1)

r2 = idx2.get_indexer(idx1[::-1], method="backfill")
assert_almost_equal(r2, e1[::-1])
tm.assert_almost_equal(r2, e1[::-1])

rbfill1 = idx2.get_indexer(idx1, method="bfill")
assert_almost_equal(r1, rbfill1)
tm.assert_almost_equal(r1, rbfill1)

# pass non-MultiIndex
r1 = idx1.get_indexer(idx2.values)
rexp1 = idx1.get_indexer(idx2)
assert_almost_equal(r1, rexp1)
tm.assert_almost_equal(r1, rexp1)

r1 = idx1.get_indexer([1, 2, 3])
assert (r1 == [-1, -1, -1]).all()
Expand Down
7 changes: 3 additions & 4 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
from pandas.tests.indexes.common import Base
from pandas.tests.indexes.conftest import indices_dict
import pandas.util.testing as tm
from pandas.util.testing import assert_almost_equal


class TestIndex(Base):
Expand Down Expand Up @@ -1452,7 +1451,7 @@ def test_get_indexer(self):

r1 = index1.get_indexer(index2)
e1 = np.array([1, 3, -1], dtype=np.intp)
assert_almost_equal(r1, e1)
tm.assert_almost_equal(r1, e1)

@pytest.mark.parametrize("reverse", [True, False])
@pytest.mark.parametrize(
Expand All @@ -1473,7 +1472,7 @@ def test_get_indexer_methods(self, reverse, expected, method):
expected = expected[::-1]

result = index2.get_indexer(index1, method=method)
assert_almost_equal(result, expected)
tm.assert_almost_equal(result, expected)

def test_get_indexer_invalid(self):
# GH10411
Expand Down Expand Up @@ -1921,7 +1920,7 @@ def test_get_value(self, index):
values = np.random.randn(100)
value = index[67]

assert_almost_equal(index.get_value(values, value), values[67])
tm.assert_almost_equal(index.get_value(values, value), values[67])

@pytest.mark.parametrize("values", [["foo", "bar", "quux"], {"foo", "bar", "quux"}])
@pytest.mark.parametrize(
Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/indexes/test_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from pandas import Categorical, IntervalIndex
from pandas.core.indexes.api import CategoricalIndex, Index
import pandas.util.testing as tm
from pandas.util.testing import assert_almost_equal

from .common import Base

Expand Down Expand Up @@ -678,7 +677,7 @@ def test_get_indexer(self):

for indexer in [idx2, list("abf"), Index(list("abf"))]:
r1 = idx1.get_indexer(idx2)
assert_almost_equal(r1, np.array([0, 1, 2, -1], dtype=np.intp))
tm.assert_almost_equal(r1, np.array([0, 1, 2, -1], dtype=np.intp))

msg = (
"method='pad' and method='backfill' not implemented yet for"
Expand Down
Loading

0 comments on commit b5c64dc

Please sign in to comment.