Skip to content

TST: collect tests by method, split large tests #40133

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 4 commits into from
Mar 1, 2021
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
2 changes: 2 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,12 +589,14 @@ def __init__(

elif isinstance(data, (np.ndarray, Series, Index)):
if data.dtype.names:
# i.e. numpy structured array
data_columns = list(data.dtype.names)
data = {k: data[k] for k in data_columns}
if columns is None:
columns = data_columns
mgr = dict_to_mgr(data, index, columns, dtype=dtype)
elif getattr(data, "name", None) is not None:
# i.e. Series/Index with non-None name
mgr = dict_to_mgr({data.name: data}, index, columns, dtype=dtype)
else:
mgr = ndarray_to_mgr(data, index, columns, dtype=dtype, copy=copy)
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1722,12 +1722,15 @@ def test_constructor_with_datetimes(self):
)
tm.assert_series_equal(result, expected)

def test_constructor_with_datetimes1(self):

# GH 2809
ind = date_range(start="2000-01-01", freq="D", periods=10)
datetimes = [ts.to_pydatetime() for ts in ind]
datetime_s = Series(datetimes)
assert datetime_s.dtype == "M8[ns]"

def test_constructor_with_datetimes2(self):
# GH 2810
ind = date_range(start="2000-01-01", freq="D", periods=10)
datetimes = [ts.to_pydatetime() for ts in ind]
Expand All @@ -1741,6 +1744,7 @@ def test_constructor_with_datetimes(self):
)
tm.assert_series_equal(result, expected)

def test_constructor_with_datetimes3(self):
# GH 7594
# don't coerce tz-aware
tz = pytz.timezone("US/Eastern")
Expand All @@ -1758,6 +1762,7 @@ def test_constructor_with_datetimes(self):
df.dtypes, Series({"End Date": "datetime64[ns, US/Eastern]"})
)

def test_constructor_with_datetimes4(self):
# tz-aware (UTC and other tz's)
# GH 8411
dr = date_range("20130101", periods=3)
Expand All @@ -1770,6 +1775,7 @@ def test_constructor_with_datetimes(self):
df = DataFrame({"value": dr})
assert str(df.iat[0, 0].tz) == "US/Eastern"

def test_constructor_with_datetimes5(self):
# GH 7822
# preserver an index with a tz on dict construction
i = date_range("1/1/2011", periods=5, freq="10s", tz="US/Eastern")
Expand All @@ -1782,7 +1788,9 @@ def test_constructor_with_datetimes(self):
df = DataFrame({"a": i})
tm.assert_frame_equal(df, expected)

def test_constructor_with_datetimes6(self):
# multiples
i = date_range("1/1/2011", periods=5, freq="10s", tz="US/Eastern")
i_no_tz = date_range("1/1/2011", periods=5, freq="10s")
df = DataFrame({"a": i, "b": i_no_tz})
expected = DataFrame({"a": i.to_series().reset_index(drop=True), "b": i_no_tz})
Expand Down
62 changes: 62 additions & 0 deletions pandas/tests/indexes/categorical/test_append.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import pytest

from pandas import (
CategoricalIndex,
Index,
)
import pandas._testing as tm


class TestAppend:
@pytest.fixture
def ci(self):
categories = list("cab")
return CategoricalIndex(list("aabbca"), categories=categories, ordered=False)

def test_append(self, ci):
# append cats with the same categories
result = ci[:3].append(ci[3:])
tm.assert_index_equal(result, ci, exact=True)

foos = [ci[:1], ci[1:3], ci[3:]]
result = foos[0].append(foos[1:])
tm.assert_index_equal(result, ci, exact=True)

def test_append_empty(self, ci):
# empty
result = ci.append([])
tm.assert_index_equal(result, ci, exact=True)

def test_append_mismatched_categories(self, ci):
# appending with different categories or reordered is not ok
msg = "all inputs must be Index"
with pytest.raises(TypeError, match=msg):
ci.append(ci.values.set_categories(list("abcd")))
with pytest.raises(TypeError, match=msg):
ci.append(ci.values.reorder_categories(list("abc")))

def test_append_category_objects(self, ci):
# with objects
result = ci.append(Index(["c", "a"]))
expected = CategoricalIndex(list("aabbcaca"), categories=ci.categories)
tm.assert_index_equal(result, expected, exact=True)

def test_append_non_categories(self, ci):
# invalid objects -> cast to object via concat_compat
result = ci.append(Index(["a", "d"]))
expected = Index(["a", "a", "b", "b", "c", "a", "a", "d"])
tm.assert_index_equal(result, expected, exact=True)

def test_append_object(self, ci):
# GH#14298 - if base object is not categorical -> coerce to object
result = Index(["c", "a"]).append(ci)
expected = Index(list("caaabbca"))
tm.assert_index_equal(result, expected, exact=True)

def test_append_to_another(self):
# hits Index._concat
fst = Index(["a", "b"])
snd = CategoricalIndex(["d", "e"])
result = fst.append(snd)
expected = Index(["a", "b", "d", "e"])
tm.assert_index_equal(result, expected)
53 changes: 0 additions & 53 deletions pandas/tests/indexes/categorical/test_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,53 +30,6 @@ def test_can_hold_identifiers(self):
key = idx[0]
assert idx._can_hold_identifiers_and_holds_name(key) is True

def test_append(self):

ci = self.create_index()
categories = ci.categories

# append cats with the same categories
result = ci[:3].append(ci[3:])
tm.assert_index_equal(result, ci, exact=True)

foos = [ci[:1], ci[1:3], ci[3:]]
result = foos[0].append(foos[1:])
tm.assert_index_equal(result, ci, exact=True)

# empty
result = ci.append([])
tm.assert_index_equal(result, ci, exact=True)

# appending with different categories or reordered is not ok
msg = "all inputs must be Index"
with pytest.raises(TypeError, match=msg):
ci.append(ci.values.set_categories(list("abcd")))
with pytest.raises(TypeError, match=msg):
ci.append(ci.values.reorder_categories(list("abc")))

# with objects
result = ci.append(Index(["c", "a"]))
expected = CategoricalIndex(list("aabbcaca"), categories=categories)
tm.assert_index_equal(result, expected, exact=True)

# invalid objects -> cast to object via concat_compat
result = ci.append(Index(["a", "d"]))
expected = Index(["a", "a", "b", "b", "c", "a", "a", "d"])
tm.assert_index_equal(result, expected, exact=True)

# GH14298 - if base object is not categorical -> coerce to object
result = Index(["c", "a"]).append(ci)
expected = Index(list("caaabbca"))
tm.assert_index_equal(result, expected, exact=True)

def test_append_to_another(self):
# hits Index._concat
fst = Index(["a", "b"])
snd = CategoricalIndex(["d", "e"])
result = fst.append(snd)
expected = Index(["a", "b", "d", "e"])
tm.assert_index_equal(result, expected)

def test_insert(self):

ci = self.create_index()
Expand Down Expand Up @@ -326,12 +279,6 @@ def test_map_str(self):
class TestCategoricalIndex2:
# Tests that are not overriding a test in Base

def test_format_different_scalar_lengths(self):
# GH35439
idx = CategoricalIndex(["aaaaaaaaa", "b"])
expected = ["aaaaaaaaa", "b"]
assert idx.format() == expected

@pytest.mark.parametrize(
"dtype, engine_type",
[
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/indexes/categorical/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@


class TestCategoricalIndexRepr:
def test_format_different_scalar_lengths(self):
# GH#35439
idx = CategoricalIndex(["aaaaaaaaa", "b"])
expected = ["aaaaaaaaa", "b"]
assert idx.format() == expected

def test_string_categorical_index_repr(self):
# short
idx = CategoricalIndex(["a", "bb", "ccc"])
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 @@ -551,6 +551,13 @@ def test_get_loc_reasonable_key_error(self):
with pytest.raises(KeyError, match="2000"):
index.get_loc("1/1/2000")

def test_get_loc_year_str(self):
rng = date_range("1/1/2000", "1/1/2010")

result = rng.get_loc("2009")
expected = slice(3288, 3653)
assert result == expected


class TestContains:
def test_dti_contains_with_duplicates(self):
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/indexes/datetimes/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ def test_range_edges(self):
)
tm.assert_index_equal(idx, exp)

def test_range_edges2(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

looks like paramterization in the future


idx = date_range(
start=Timestamp("1970-01-01 00:00:00.000000004"),
end=Timestamp("1970-01-01 00:00:00.000000001"),
Expand All @@ -45,6 +47,8 @@ def test_range_edges(self):
exp = DatetimeIndex([], freq="N")
tm.assert_index_equal(idx, exp)

def test_range_edges3(self):

idx = date_range(
start=Timestamp("1970-01-01 00:00:00.000000001"),
end=Timestamp("1970-01-01 00:00:00.000000001"),
Expand All @@ -53,6 +57,8 @@ def test_range_edges(self):
exp = DatetimeIndex(["1970-01-01 00:00:00.000000001"], freq="N")
tm.assert_index_equal(idx, exp)

def test_range_edges4(self):

idx = date_range(
start=Timestamp("1970-01-01 00:00:00.000001"),
end=Timestamp("1970-01-01 00:00:00.000004"),
Expand All @@ -69,6 +75,8 @@ def test_range_edges(self):
)
tm.assert_index_equal(idx, exp)

def test_range_edges5(self):

idx = date_range(
start=Timestamp("1970-01-01 00:00:00.001"),
end=Timestamp("1970-01-01 00:00:00.004"),
Expand All @@ -85,6 +93,7 @@ def test_range_edges(self):
)
tm.assert_index_equal(idx, exp)

def test_range_edges6(self):
idx = date_range(
start=Timestamp("1970-01-01 00:00:01"),
end=Timestamp("1970-01-01 00:00:04"),
Expand All @@ -101,6 +110,7 @@ def test_range_edges(self):
)
tm.assert_index_equal(idx, exp)

def test_range_edges7(self):
idx = date_range(
start=Timestamp("1970-01-01 00:01"),
end=Timestamp("1970-01-01 00:04"),
Expand All @@ -117,6 +127,7 @@ def test_range_edges(self):
)
tm.assert_index_equal(idx, exp)

def test_range_edges8(self):
idx = date_range(
start=Timestamp("1970-01-01 01:00"),
end=Timestamp("1970-01-01 04:00"),
Expand All @@ -133,6 +144,7 @@ def test_range_edges(self):
)
tm.assert_index_equal(idx, exp)

def test_range_edges9(self):
idx = date_range(
start=Timestamp("1970-01-01"), end=Timestamp("1970-01-04"), freq="D"
)
Expand Down Expand Up @@ -234,24 +246,28 @@ def test_datetimeindex_accessors(self):
exp = DatetimeIndex([], freq="D", tz=dti.tz, name="name")
tm.assert_index_equal(res, exp)

def test_datetimeindex_accessors2(self):
dti = date_range(freq="BQ-FEB", start=datetime(1998, 1, 1), periods=4)

assert sum(dti.is_quarter_start) == 0
assert sum(dti.is_quarter_end) == 4
assert sum(dti.is_year_start) == 0
assert sum(dti.is_year_end) == 1

def test_datetimeindex_accessors3(self):
# Ensure is_start/end accessors throw ValueError for CustomBusinessDay,
bday_egypt = offsets.CustomBusinessDay(weekmask="Sun Mon Tue Wed Thu")
dti = date_range(datetime(2013, 4, 30), periods=5, freq=bday_egypt)
msg = "Custom business days is not supported by is_month_start"
with pytest.raises(ValueError, match=msg):
dti.is_month_start

def test_datetimeindex_accessors4(self):
dti = DatetimeIndex(["2000-01-01", "2000-01-02", "2000-01-03"])

assert dti.is_month_start[0] == 1

def test_datetimeindex_accessors5(self):
tests = [
(Timestamp("2013-06-01", freq="M").is_month_start, 1),
(Timestamp("2013-06-01", freq="BM").is_month_start, 0),
Expand Down Expand Up @@ -290,6 +306,7 @@ def test_datetimeindex_accessors(self):
for ts, value in tests:
assert ts == value

def test_datetimeindex_accessors6(self):
# GH 6538: Check that DatetimeIndex and its TimeStamp elements
# return the same weekofyear accessor close to new year w/ tz
dates = ["2013/12/29", "2013/12/30", "2013/12/31"]
Expand Down
6 changes: 0 additions & 6 deletions pandas/tests/indexes/datetimes/test_partial_slicing.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,6 @@ def test_slice_year(self):
expected = df[df.index.year == 2005]
tm.assert_frame_equal(result, expected)

rng = date_range("1/1/2000", "1/1/2010")

result = rng.get_loc("2009")
expected = slice(3288, 3653)
assert result == expected

@pytest.mark.parametrize(
"partial_dtime",
[
Expand Down
15 changes: 0 additions & 15 deletions pandas/tests/indexes/period/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,6 @@ def test_is_(self):
assert not index.is_(index - 2)
assert not index.is_(index - 0)

def test_periods_number_check(self):
msg = (
"Of the three parameters: start, end, and periods, exactly two "
"must be specified"
)
with pytest.raises(ValueError, match=msg):
period_range("2011-1-1", "2012-1-1", "B")

def test_index_duplicate_periods(self):
# monotonic
idx = PeriodIndex([2000, 2007, 2007, 2009, 2009], freq="A-JUN")
Expand Down Expand Up @@ -348,13 +340,6 @@ def test_with_multi_index(self):

assert isinstance(s.index.values[0][0], Period)

def test_convert_array_of_periods(self):
rng = period_range("1/1/2000", periods=20, freq="D")
periods = list(rng)

result = Index(periods)
assert isinstance(result, PeriodIndex)

def test_pickle_freq(self):
# GH2891
prng = period_range("1/1/2011", "1/1/2012", freq="M")
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/indexes/period/test_period_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@


class TestPeriodRange:
def test_required_arguments(self):
msg = (
"Of the three parameters: start, end, and periods, exactly two "
"must be specified"
)
with pytest.raises(ValueError, match=msg):
period_range("2011-1-1", "2012-1-1", "B")

@pytest.mark.parametrize("freq", ["D", "W", "M", "Q", "A"])
def test_construction_from_string(self, freq):
# non-empty
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/indexes/test_index_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ def test_constructor_infer_periodindex(self):
tm.assert_index_equal(rs, xp)
assert isinstance(rs, PeriodIndex)

def test_from_list_of_periods(self):
rng = period_range("1/1/2000", periods=20, freq="D")
periods = list(rng)

result = Index(periods)
assert isinstance(result, PeriodIndex)

@pytest.mark.parametrize("pos", [0, 1])
@pytest.mark.parametrize(
"klass,dtype,ctor",
Expand Down
Loading