Skip to content

TST: move misplaced tests #55603

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 6 commits into from
Oct 20, 2023
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
8 changes: 0 additions & 8 deletions pandas/tests/arrays/test_datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -746,14 +746,6 @@ def test_iter_zoneinfo_fold(self, tz):
assert str(left) == str(right2)
assert left.utcoffset() == right2.utcoffset()

def test_date_range_frequency_M_deprecated(self):
depr_msg = "'M' will be deprecated, please use 'ME' instead."

expected = pd.date_range("1/1/2000", periods=4, freq="2ME")
with tm.assert_produces_warning(UserWarning, match=depr_msg):
result = pd.date_range("1/1/2000", periods=4, freq="2M")
tm.assert_index_equal(result, expected)


def test_factorize_sort_without_freq():
dta = DatetimeArray._from_sequence([0, 2, 1])
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/frame/methods/test_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
)
import pandas._testing as tm

from pandas.tseries.offsets import BDay


def test_map(float_frame):
result = float_frame.map(lambda x: x * 2)
Expand Down Expand Up @@ -158,8 +160,6 @@ def test_map_box():


def test_frame_map_dont_convert_datetime64():
from pandas.tseries.offsets import BDay

df = DataFrame({"x1": [datetime(1996, 1, 1)]})

df = df.map(lambda x: x + BDay())
Expand Down
10 changes: 6 additions & 4 deletions pandas/tests/groupby/test_grouping.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ def test_grouper_creation_bug(self):
expected = expected.loc[:, ["A", "B"]]
tm.assert_frame_equal(result, expected)

def test_grouper_creation_bug2(self):
# GH14334
# Grouper(key=...) may be passed in a list
df = DataFrame(
Expand Down Expand Up @@ -275,24 +276,25 @@ def test_grouper_creation_bug(self):
result = g.sum()
tm.assert_frame_equal(result, expected)

def test_grouper_creation_bug3(self):
# GH8866
s = Series(
ser = Series(
np.arange(8, dtype="int64"),
index=MultiIndex.from_product(
[list("ab"), range(2), date_range("20130101", periods=2)],
names=["one", "two", "three"],
),
)
result = s.groupby(Grouper(level="three", freq="ME")).sum()
result = ser.groupby(Grouper(level="three", freq="ME")).sum()
expected = Series(
[28],
index=pd.DatetimeIndex([Timestamp("2013-01-31")], freq="ME", name="three"),
)
tm.assert_series_equal(result, expected)

# just specifying a level breaks
result = s.groupby(Grouper(level="one")).sum()
expected = s.groupby(level="one").sum()
result = ser.groupby(Grouper(level="one")).sum()
expected = ser.groupby(level="one").sum()
tm.assert_series_equal(result, expected)

def test_grouper_column_and_index(self):
Expand Down
6 changes: 0 additions & 6 deletions pandas/tests/indexes/datetimes/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1110,9 +1110,3 @@ def test_pass_datetimeindex_to_index(self):
expected = Index(rng.to_pydatetime(), dtype=object)

tm.assert_numpy_array_equal(idx.values, expected.values)

def test_date_range_tuple_freq_raises(self):
# GH#34703
edate = datetime(2000, 1, 1)
with pytest.raises(TypeError, match="pass as a string instead"):
date_range(end=edate, freq=("D", 5), periods=20)
53 changes: 53 additions & 0 deletions pandas/tests/indexes/datetimes/test_date_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,20 @@ def test_date_range_timestamp_equiv_preserve_frequency(self):


class TestDateRanges:
def test_date_range_frequency_M_deprecated(self):
depr_msg = "'M' will be deprecated, please use 'ME' instead."

expected = date_range("1/1/2000", periods=4, freq="2ME")
with tm.assert_produces_warning(UserWarning, match=depr_msg):
result = date_range("1/1/2000", periods=4, freq="2M")
tm.assert_index_equal(result, expected)

def test_date_range_tuple_freq_raises(self):
# GH#34703
edate = datetime(2000, 1, 1)
with pytest.raises(TypeError, match="pass as a string instead"):
date_range(end=edate, freq=("D", 5), periods=20)

@pytest.mark.parametrize("freq", ["ns", "us", "ms", "min", "s", "h", "D"])
def test_date_range_edges(self, freq):
# GH#13672
Expand Down Expand Up @@ -911,6 +925,45 @@ def test_date_range_with_tz(self, tzstr):

assert stamp == rng[1]

@pytest.mark.parametrize("tz", ["Europe/London", "dateutil/Europe/London"])
def test_date_range_ambiguous_endpoint(self, tz):
# construction with an ambiguous end-point
# GH#11626

with pytest.raises(pytz.AmbiguousTimeError, match="Cannot infer dst time"):
date_range(
"2013-10-26 23:00", "2013-10-27 01:00", tz="Europe/London", freq="h"
)

times = date_range(
"2013-10-26 23:00", "2013-10-27 01:00", freq="h", tz=tz, ambiguous="infer"
)
assert times[0] == Timestamp("2013-10-26 23:00", tz=tz)
assert times[-1] == Timestamp("2013-10-27 01:00:00+0000", tz=tz)

@pytest.mark.parametrize(
"tz, option, expected",
[
["US/Pacific", "shift_forward", "2019-03-10 03:00"],
["dateutil/US/Pacific", "shift_forward", "2019-03-10 03:00"],
["US/Pacific", "shift_backward", "2019-03-10 01:00"],
["dateutil/US/Pacific", "shift_backward", "2019-03-10 01:00"],
["US/Pacific", timedelta(hours=1), "2019-03-10 03:00"],
],
)
def test_date_range_nonexistent_endpoint(self, tz, option, expected):
# construction with an nonexistent end-point

with pytest.raises(pytz.NonExistentTimeError, match="2019-03-10 02:00:00"):
date_range(
"2019-03-10 00:00", "2019-03-10 02:00", tz="US/Pacific", freq="h"
)

times = date_range(
"2019-03-10 00:00", "2019-03-10 02:00", freq="h", tz=tz, nonexistent=option
)
assert times[-1] == Timestamp(expected, tz=tz)


class TestGenRangeGeneration:
def test_generate(self):
Expand Down
13 changes: 0 additions & 13 deletions pandas/tests/indexes/datetimes/test_datetimelike.py

This file was deleted.

67 changes: 62 additions & 5 deletions pandas/tests/indexes/datetimes/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pandas as pd
from pandas import (
DatetimeIndex,
NaT,
Series,
)
import pandas._testing as tm
Expand All @@ -33,7 +34,7 @@ def test_get_values_for_csv():
tm.assert_numpy_array_equal(result, expected)

# NULL object handling should work
index = DatetimeIndex(["2017-01-01", pd.NaT, "2017-01-03"])
index = DatetimeIndex(["2017-01-01", NaT, "2017-01-03"])
expected = np.array(["2017-01-01", "NaT", "2017-01-03"], dtype=object)

result = index._get_values_for_csv(na_rep="NaT")
Expand All @@ -58,6 +59,20 @@ def test_get_values_for_csv():


class TestDatetimeIndexRendering:
def test_dti_repr_dates(self):
text = str(pd.to_datetime([datetime(2013, 1, 1), datetime(2014, 1, 1)]))
assert "['2013-01-01'," in text
assert ", '2014-01-01']" in text

def test_dti_repr_mixed(self):
text = str(
pd.to_datetime(
[datetime(2013, 1, 1), datetime(2014, 1, 1, 12), datetime(2014, 1, 1)]
)
)
assert "'2013-01-01 00:00:00'," in text
assert "'2014-01-01 00:00:00']" in text

def test_dti_repr_short(self):
dr = pd.date_range(start="1/1/2012", periods=1)
repr(dr)
Expand Down Expand Up @@ -114,11 +129,11 @@ def test_dti_representation(self, method):
)
idxs.append(
DatetimeIndex(
["2011-01-01 09:00", "2011-01-01 10:00", pd.NaT], tz="US/Eastern"
["2011-01-01 09:00", "2011-01-01 10:00", NaT], tz="US/Eastern"
)
)
idxs.append(
DatetimeIndex(["2011-01-01 09:00", "2011-01-01 10:00", pd.NaT], tz="UTC")
DatetimeIndex(["2011-01-01 09:00", "2011-01-01 10:00", NaT], tz="UTC")
)

exp = []
Expand Down Expand Up @@ -165,7 +180,7 @@ def test_dti_representation_to_series(self):
tz="Asia/Tokyo",
)
idx6 = DatetimeIndex(
["2011-01-01 09:00", "2011-01-01 10:00", pd.NaT], tz="US/Eastern"
["2011-01-01 09:00", "2011-01-01 10:00", NaT], tz="US/Eastern"
)
idx7 = DatetimeIndex(["2011-01-01 09:00", "2011-01-02 10:15"])

Expand Down Expand Up @@ -222,7 +237,7 @@ def test_dti_summary(self):
tz="Asia/Tokyo",
)
idx6 = DatetimeIndex(
["2011-01-01 09:00", "2011-01-01 10:00", pd.NaT], tz="US/Eastern"
["2011-01-01 09:00", "2011-01-01 10:00", NaT], tz="US/Eastern"
)

exp1 = "DatetimeIndex: 0 entries\nFreq: D"
Expand Down Expand Up @@ -281,6 +296,14 @@ def test_dti_custom_business_summary_dateutil(self):


class TestFormat:
def test_format(self):
# GH#35439
idx = pd.date_range("20130101", periods=5)
expected = [f"{x:%Y-%m-%d}" for x in idx]
msg = r"DatetimeIndex\.format is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
assert idx.format() == expected

def test_format_with_name_time_info(self):
# bug I fixed 12/20/2011
dates = pd.date_range("2011-01-01 04:00:00", periods=10, name="something")
Expand All @@ -299,3 +322,37 @@ def test_format_datetime_with_time(self):
expected = ["2012-02-07 00:00:00", "2012-02-07 23:00:00"]
assert len(result) == 2
assert result == expected

def test_format_datetime(self):
msg = "DatetimeIndex.format is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
formatted = pd.to_datetime([datetime(2003, 1, 1, 12), NaT]).format()
assert formatted[0] == "2003-01-01 12:00:00"
assert formatted[1] == "NaT"

def test_format_date(self):
msg = "DatetimeIndex.format is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
formatted = pd.to_datetime([datetime(2003, 1, 1), NaT]).format()
assert formatted[0] == "2003-01-01"
assert formatted[1] == "NaT"

def test_format_date_tz(self):
dti = pd.to_datetime([datetime(2013, 1, 1)], utc=True)
msg = "DatetimeIndex.format is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
formatted = dti.format()
assert formatted[0] == "2013-01-01 00:00:00+00:00"

dti = pd.to_datetime([datetime(2013, 1, 1), NaT], utc=True)
with tm.assert_produces_warning(FutureWarning, match=msg):
formatted = dti.format()
assert formatted[0] == "2013-01-01 00:00:00+00:00"

def test_format_date_explicit_date_format(self):
dti = pd.to_datetime([datetime(2003, 2, 1), NaT])
msg = "DatetimeIndex.format is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
formatted = dti.format(date_format="%m-%d-%Y", na_rep="UT")
assert formatted[0] == "02-01-2003"
assert formatted[1] == "UT"
66 changes: 57 additions & 9 deletions pandas/tests/indexes/datetimes/test_setops.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from datetime import datetime
from datetime import (
datetime,
timezone,
)

import numpy as np
import pytest
Expand All @@ -12,6 +15,7 @@
DatetimeIndex,
Index,
Series,
Timestamp,
bdate_range,
date_range,
)
Expand Down Expand Up @@ -416,6 +420,52 @@ def test_intersection_non_tick_no_fastpath(self):
expected = dti[:0]
tm.assert_index_equal(result, expected)

def test_dti_intersection(self):
rng = date_range("1/1/2011", periods=100, freq="h", tz="utc")

left = rng[10:90][::-1]
right = rng[20:80][::-1]

assert left.tz == rng.tz
result = left.intersection(right)
assert result.tz == left.tz

# Note: not difference, as there is no symmetry requirement there
@pytest.mark.parametrize("setop", ["union", "intersection", "symmetric_difference"])
def test_dti_setop_aware(self, setop):
# non-overlapping
# GH#39328 as of 2.0 we cast these to UTC instead of object
rng = date_range("2012-11-15 00:00:00", periods=6, freq="h", tz="US/Central")

rng2 = date_range("2012-11-15 12:00:00", periods=6, freq="h", tz="US/Eastern")

result = getattr(rng, setop)(rng2)

left = rng.tz_convert("UTC")
right = rng2.tz_convert("UTC")
expected = getattr(left, setop)(right)
tm.assert_index_equal(result, expected)
assert result.tz == left.tz
if len(result):
assert result[0].tz is timezone.utc
assert result[-1].tz is timezone.utc

def test_dti_union_mixed(self):
# GH#21671
rng = DatetimeIndex([Timestamp("2011-01-01"), pd.NaT])
rng2 = DatetimeIndex(["2012-01-01", "2012-01-02"], tz="Asia/Tokyo")
result = rng.union(rng2)
expected = Index(
[
Timestamp("2011-01-01"),
pd.NaT,
Timestamp("2012-01-01", tz="Asia/Tokyo"),
Timestamp("2012-01-02", tz="Asia/Tokyo"),
],
dtype=object,
)
tm.assert_index_equal(result, expected)


class TestBusinessDatetimeIndex:
def test_union(self, sort):
Expand Down Expand Up @@ -500,15 +550,13 @@ def test_intersection_bug(self):
def test_intersection_list(self):
# GH#35876
# values is not an Index -> no name -> retain "a"
values = [pd.Timestamp("2020-01-01"), pd.Timestamp("2020-02-01")]
values = [Timestamp("2020-01-01"), Timestamp("2020-02-01")]
idx = DatetimeIndex(values, name="a")
res = idx.intersection(values)
tm.assert_index_equal(res, idx)

def test_month_range_union_tz_pytz(self, sort):
from pytz import timezone

tz = timezone("US/Eastern")
tz = pytz.timezone("US/Eastern")

early_start = datetime(2011, 1, 1)
early_end = datetime(2011, 3, 1)
Expand Down Expand Up @@ -543,13 +591,13 @@ def test_intersection_duplicates(self, sort):
# GH#38196
idx1 = Index(
[
pd.Timestamp("2019-12-13"),
pd.Timestamp("2019-12-12"),
pd.Timestamp("2019-12-12"),
Timestamp("2019-12-13"),
Timestamp("2019-12-12"),
Timestamp("2019-12-12"),
]
)
result = idx1.intersection(idx1, sort=sort)
expected = Index([pd.Timestamp("2019-12-13"), pd.Timestamp("2019-12-12")])
expected = Index([Timestamp("2019-12-13"), Timestamp("2019-12-12")])
tm.assert_index_equal(result, expected)


Expand Down
Loading