Skip to content

REF/TST: misplaced tests in test_timeseries, test_timezones #32300

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 7 commits into from
Feb 27, 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
19 changes: 19 additions & 0 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import numpy.ma as ma
import numpy.ma.mrecords as mrecords
import pytest
import pytz

from pandas.compat import is_platform_little_endian
from pandas.compat.numpy import _is_numpy_dev
Expand Down Expand Up @@ -2389,6 +2390,12 @@ def test_from_records_series_list_dict(self):
result = DataFrame.from_records(data)
tm.assert_frame_equal(result, expected)

def test_frame_from_records_utc(self):
rec = {"datum": 1.5, "begin_time": datetime(2006, 4, 27, tzinfo=pytz.utc)}

# it works
DataFrame.from_records([rec], index="begin_time")

def test_to_frame_with_falsey_names(self):
# GH 16114
result = Series(name=0, dtype=object).to_frame().dtypes
Expand Down Expand Up @@ -2460,6 +2467,18 @@ def test_construct_with_two_categoricalindex_series(self):
)
tm.assert_frame_equal(result, expected)

def test_from_M8_structured(self):
dates = [(datetime(2012, 9, 9, 0, 0), datetime(2012, 9, 8, 15, 10))]
arr = np.array(dates, dtype=[("Date", "M8[us]"), ("Forecasting", "M8[us]")])
df = DataFrame(arr)

assert df["Date"][0] == dates[0][0]
assert df["Forecasting"][0] == dates[0][1]

s = Series(arr["Date"])
Copy link
Member

Choose a reason for hiding this comment

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

maybe add a TODO for spliting out the Series test or parameterising.

as an aside where will the Series and Frame tests go. does this need to move again?

Copy link
Member Author

Choose a reason for hiding this comment

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

as an aside where will the Series and Frame tests go

For tests like this that are mostly testing Frame with a little bit of Series test on the side, it'll probably stay mixed for the foreseeable future. For tests where we can share/parametrize tests between Series/Frame, maybe tests.generic? (though the tests currently in that directory are not in great shape)

assert isinstance(s[0], Timestamp)
assert s[0] == dates[0][0]


class TestDataFrameConstructorWithDatetimeTZ:
def test_from_dict(self):
Expand Down
8 changes: 0 additions & 8 deletions pandas/tests/frame/test_timezones.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"""
Tests for DataFrame timezone-related methods
"""
from datetime import datetime

import numpy as np
import pytest
import pytz
Expand Down Expand Up @@ -53,12 +51,6 @@ def test_frame_values_with_tz(self):
result = df.values
tm.assert_numpy_array_equal(result, expected)

def test_frame_from_records_utc(self):
rec = {"datum": 1.5, "begin_time": datetime(2006, 4, 27, tzinfo=pytz.utc)}

# it works
DataFrame.from_records([rec], index="begin_time")

def test_frame_join_tzaware(self):
test1 = DataFrame(
np.zeros((6, 3)),
Expand Down
20 changes: 19 additions & 1 deletion pandas/tests/indexes/datetimes/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest

import pandas as pd
from pandas import DataFrame, DatetimeIndex, Index, Timestamp, date_range, offsets
from pandas import DataFrame, DatetimeIndex, Index, NaT, Timestamp, date_range, offsets
import pandas._testing as tm

randn = np.random.randn
Expand All @@ -20,6 +20,24 @@ def test_roundtrip_pickle_with_tz(self):
unpickled = tm.round_trip_pickle(index)
tm.assert_index_equal(index, unpickled)

def test_pickle(self):

# GH#4606
p = tm.round_trip_pickle(NaT)
assert p is NaT

idx = pd.to_datetime(["2013-01-01", NaT, "2014-01-06"])
idx_p = tm.round_trip_pickle(idx)
assert idx_p[0] == idx[0]
assert idx_p[1] is NaT
assert idx_p[2] == idx[2]

# GH#11002
# don't infer freq
idx = date_range("1750-1-1", "2050-1-1", freq="7D")
idx_p = tm.round_trip_pickle(idx)
tm.assert_index_equal(idx, idx_p)

def test_reindex_preserves_tz_if_target_is_empty_list_or_array(self):
# GH7774
index = date_range("20130101", periods=3, tz="US/Eastern")
Expand Down
35 changes: 35 additions & 0 deletions pandas/tests/indexes/datetimes/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,41 @@ def test_datetimeindex_diff(self, sort):
dti2 = date_range(freq="Q-JAN", start=datetime(1997, 12, 31), periods=98)
assert len(dti1.difference(dti2, sort)) == 2

@pytest.mark.parametrize("tz", [None, "Asia/Tokyo", "US/Eastern"])
def test_setops_preserve_freq(self, tz):
rng = date_range("1/1/2000", "1/1/2002", name="idx", tz=tz)

result = rng[:50].union(rng[50:100])
assert result.name == rng.name
assert result.freq == rng.freq
assert result.tz == rng.tz

result = rng[:50].union(rng[30:100])
assert result.name == rng.name
assert result.freq == rng.freq
assert result.tz == rng.tz

result = rng[:50].union(rng[60:100])
assert result.name == rng.name
assert result.freq is None
assert result.tz == rng.tz

result = rng[:50].intersection(rng[25:75])
assert result.name == rng.name
assert result.freqstr == "D"
assert result.tz == rng.tz

nofreq = DatetimeIndex(list(rng[25:75]), name="other")
result = rng[:50].union(nofreq)
assert result.name is None
assert result.freq == rng.freq
assert result.tz == rng.tz

result = rng[:50].intersection(nofreq)
assert result.name is None
assert result.freq == rng.freq
assert result.tz == rng.tz


class TestBusinessDatetimeIndex:
def setup_method(self, method):
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/indexes/multi/test_get_level_values.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from pandas import MultiIndex, Timestamp, date_range


class TestGetLevelValues:
Copy link
Member

Choose a reason for hiding this comment

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

This method is also on Index. should the module be just pandas/tests/indexes/test_get_level_values.py

Copy link
Member

Choose a reason for hiding this comment

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

sorry... pandas/tests/indexes/methods/test_get_level_values.py

Copy link
Member Author

Choose a reason for hiding this comment

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

it exists on Index, but is really only relevant for MultiIndex i think

Copy link
Member

Choose a reason for hiding this comment

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

I appreciate that, but I assume that this reorganization is to aid parametrisation. and that the index tests will be grouped by method instead of Index class?

Copy link
Member Author

Choose a reason for hiding this comment

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

the index tests will be grouped by method instead of Index class

I have no plans to do that anytime soon

Copy link
Member

Choose a reason for hiding this comment

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

fair enough, I suppose that Series and Frame are still separate and that's only two classes. we'll see how the method based testing and parametrisation goes on those.

def test_get_level_values_box_datetime64(self):

dates = date_range("1/1/2000", periods=4)
levels = [dates, [0, 1]]
codes = [[0, 0, 1, 1, 2, 2, 3, 3], [0, 1, 0, 1, 0, 1, 0, 1]]

index = MultiIndex(levels=levels, codes=codes)

assert isinstance(index.get_level_values(0)[0], Timestamp)
7 changes: 7 additions & 0 deletions pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1421,3 +1421,10 @@ def test_constructor_tz_mixed_data(self):
result = Series(dt_list)
expected = Series(dt_list, dtype=object)
tm.assert_series_equal(result, expected)

def test_constructor_data_aware_dtype_naive(self, tz_aware_fixture):
# GH#25843
tz = tz_aware_fixture
result = Series([Timestamp("2019", tz=tz)], dtype="datetime64[ns]")
expected = Series([Timestamp("2019")])
tm.assert_series_equal(result, expected)
88 changes: 1 addition & 87 deletions pandas/tests/series/test_timeseries.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
from datetime import datetime
from io import StringIO

import numpy as np
import pytest

from pandas._libs.tslib import iNaT

import pandas as pd
from pandas import (
DataFrame,
DatetimeIndex,
NaT,
Series,
Timestamp,
date_range,
timedelta_range,
)
from pandas import DataFrame, DatetimeIndex, Series, date_range, timedelta_range
import pandas._testing as tm


Expand Down Expand Up @@ -225,82 +215,6 @@ def test_asfreq_resample_set_correct_freq(self):
# does .resample() set .freq correctly?
assert df.resample("D").asfreq().index.freq == "D"

def test_pickle(self):

# GH4606
p = tm.round_trip_pickle(NaT)
assert p is NaT

idx = pd.to_datetime(["2013-01-01", NaT, "2014-01-06"])
idx_p = tm.round_trip_pickle(idx)
assert idx_p[0] == idx[0]
assert idx_p[1] is NaT
assert idx_p[2] == idx[2]

# GH11002
# don't infer freq
idx = date_range("1750-1-1", "2050-1-1", freq="7D")
idx_p = tm.round_trip_pickle(idx)
tm.assert_index_equal(idx, idx_p)

@pytest.mark.parametrize("tz", [None, "Asia/Tokyo", "US/Eastern"])
def test_setops_preserve_freq(self, tz):
rng = date_range("1/1/2000", "1/1/2002", name="idx", tz=tz)

result = rng[:50].union(rng[50:100])
assert result.name == rng.name
assert result.freq == rng.freq
assert result.tz == rng.tz

result = rng[:50].union(rng[30:100])
assert result.name == rng.name
assert result.freq == rng.freq
assert result.tz == rng.tz

result = rng[:50].union(rng[60:100])
assert result.name == rng.name
assert result.freq is None
assert result.tz == rng.tz

result = rng[:50].intersection(rng[25:75])
assert result.name == rng.name
assert result.freqstr == "D"
assert result.tz == rng.tz

nofreq = DatetimeIndex(list(rng[25:75]), name="other")
result = rng[:50].union(nofreq)
assert result.name is None
assert result.freq == rng.freq
assert result.tz == rng.tz

result = rng[:50].intersection(nofreq)
assert result.name is None
assert result.freq == rng.freq
assert result.tz == rng.tz

def test_from_M8_structured(self):
dates = [(datetime(2012, 9, 9, 0, 0), datetime(2012, 9, 8, 15, 10))]
arr = np.array(dates, dtype=[("Date", "M8[us]"), ("Forecasting", "M8[us]")])
df = DataFrame(arr)

assert df["Date"][0] == dates[0][0]
assert df["Forecasting"][0] == dates[0][1]

s = Series(arr["Date"])
assert isinstance(s[0], Timestamp)
assert s[0] == dates[0][0]

def test_get_level_values_box(self):
from pandas import MultiIndex

dates = date_range("1/1/2000", periods=4)
levels = [dates, [0, 1]]
codes = [[0, 0, 1, 1, 2, 2, 3, 3], [0, 1, 0, 1, 0, 1, 0, 1]]

index = MultiIndex(levels=levels, codes=codes)

assert isinstance(index.get_level_values(0)[0], Timestamp)

def test_view_tz(self):
# GH#24024
ser = pd.Series(pd.date_range("2000", periods=4, tz="US/Central"))
Expand Down
7 changes: 0 additions & 7 deletions pandas/tests/series/test_timezones.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,3 @@ def test_tz_localize_convert_copy_inplace_mutate(self, copy, method, tz):
np.arange(0, 5), index=date_range("20131027", periods=5, freq="1H", tz=tz)
)
tm.assert_series_equal(result, expected)

def test_constructor_data_aware_dtype_naive(self, tz_aware_fixture):
# GH 25843
tz = tz_aware_fixture
result = Series([Timestamp("2019", tz=tz)], dtype="datetime64[ns]")
expected = Series([Timestamp("2019")])
tm.assert_series_equal(result, expected)