Skip to content

Commit d33c48d

Browse files
committed
try to fix failing tests
1 parent 8f6f888 commit d33c48d

File tree

10 files changed

+81
-27
lines changed

10 files changed

+81
-27
lines changed

pandas/_libs/tslibs/period.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# -*- coding: utf-8 -*-
22
# cython: profile=False
3-
import pandas as pd
43
from datetime import datetime, date, timedelta
54

65
from cpython cimport (
@@ -39,6 +38,7 @@ cimport util
3938
from util cimport is_period_object, is_string_object, INT32_MIN
4039

4140
from pandas._libs.missing cimport is_null_datetimelike
41+
from pandas._libs.tslib import Timedelta
4242

4343
from timestamps import Timestamp
4444
from timezones cimport is_utc, is_tzlocal, get_utcoffset, get_dst_info
@@ -1198,7 +1198,7 @@ cdef class _Period(object):
11981198

11991199
end = how == 'E'
12001200
if end:
1201-
return (self + 1).to_timestamp(how='start') - pd.Timedelta(1, 'ns')
1201+
return (self + 1).to_timestamp(how='start') - Timedelta(1, 'ns')
12021202

12031203
if freq is None:
12041204
base, mult = get_freq_code(self.freq)

pandas/core/indexes/datetimes.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
import pandas.core.tools.datetimes as tools
5454

5555
from pandas._libs import (lib, index as libindex, tslib as libts,
56-
join as libjoin, Timestamp)
56+
join as libjoin, Timestamp, Timedelta)
5757
from pandas._libs.tslibs import (timezones, conversion, fields, parsing,
5858
resolution as libresolution)
5959

@@ -2226,18 +2226,27 @@ def _generate_regular_range(start, end, periods, offset):
22262226
data = DatetimeIndex._simple_new(data, None, tz=tz)
22272227
else:
22282228
if isinstance(start, Timestamp):
2229-
start = start.to_pydatetime()
2229+
start_date = start.to_pydatetime()
2230+
else:
2231+
start_date = start
22302232

22312233
if isinstance(end, Timestamp):
2232-
end = end.to_pydatetime()
2234+
end_date = end.to_pydatetime()
2235+
else:
2236+
end_date = end
22332237

2234-
xdr = generate_range(start=start, end=end,
2238+
xdr = generate_range(start=start_date, end=end_date,
22352239
periods=periods, offset=offset)
22362240

22372241
dates = list(xdr)
22382242
# utc = len(dates) > 0 and dates[0].tzinfo is not None
22392243
data = tools.to_datetime(dates)
22402244

2245+
# Add back in the lost nanoseconds
2246+
if isinstance(start, Timestamp) and isinstance(end, Timestamp):
2247+
if start.nanosecond == 999 and end.nanosecond == 999:
2248+
data = data + Timedelta(999, 'ns')
2249+
22412250
return data
22422251

22432252

pandas/core/indexes/period.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from datetime import datetime, timedelta
33
import numpy as np
44
import warnings
5-
import pandas as pd
65

76
from pandas.core import common as com
87
from pandas.core.dtypes.common import (
@@ -35,7 +34,7 @@
3534
_validate_end_alias, _quarter_to_myear)
3635
from pandas._libs.tslibs.fields import isleapyear_arr
3736
from pandas._libs.tslibs import resolution, period
38-
from pandas._libs.tslibs.timedeltas import delta_to_nanoseconds
37+
from pandas._libs.tslibs.timedeltas import delta_to_nanoseconds, Timedelta
3938

4039
from pandas.core.base import _shared_docs
4140
from pandas.core.indexes.base import _index_shared_docs, _ensure_index
@@ -677,7 +676,10 @@ def to_timestamp(self, freq=None, how='start'):
677676

678677
end = how == 'E'
679678
if end:
680-
return (self + 1).to_timestamp(how='start') - pd.Timedelta(1, 'ns')
679+
if freq == 'B':
680+
return self.to_timestamp(how='start') + Timedelta(1, 'D') - Timedelta(1, 'ns')
681+
else:
682+
return (self + 1).to_timestamp(how='start') - Timedelta(1, 'ns')
681683

682684
if freq is None:
683685
base, mult = _gfc(self.freq)
@@ -688,7 +690,17 @@ def to_timestamp(self, freq=None, how='start'):
688690
base, mult = _gfc(freq)
689691
new_data = self.asfreq(freq, how)
690692

691-
new_data = period.periodarr_to_dt64arr(new_data._values, base)
693+
end = how == 'E'
694+
if end:
695+
indexer = np.where(new_data.notnull())
696+
# move forward one period
697+
new_data._values[indexer] += 1
698+
new_data = period.periodarr_to_dt64arr(new_data._ndarray_values, base)
699+
# subtract one nanosecond
700+
new_data[indexer] -= 1
701+
else:
702+
new_data = period.periodarr_to_dt64arr(new_data._ndarray_values, base)
703+
692704
return DatetimeIndex(new_data, freq='infer', name=self.name)
693705

694706
def _maybe_convert_timedelta(self, other):

pandas/tests/frame/test_period.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import pandas as pd
66
import pandas.util.testing as tm
77
from pandas import (PeriodIndex, period_range, DataFrame, date_range,
8-
Index, to_datetime, DatetimeIndex)
8+
Index, to_datetime, DatetimeIndex, Timedelta)
99

1010

1111
def _permute(obj):
@@ -51,6 +51,7 @@ def test_frame_to_time_stamp(self):
5151
df['mix'] = 'a'
5252

5353
exp_index = date_range('1/1/2001', end='12/31/2009', freq='A-DEC')
54+
exp_index = exp_index + Timedelta(1, 'D') - Timedelta(1, 'ns')
5455
result = df.to_timestamp('D', 'end')
5556
tm.assert_index_equal(result.index, exp_index)
5657
tm.assert_numpy_array_equal(result.values, df.values)
@@ -66,22 +67,26 @@ def _get_with_delta(delta, freq='A-DEC'):
6667
delta = timedelta(hours=23)
6768
result = df.to_timestamp('H', 'end')
6869
exp_index = _get_with_delta(delta)
70+
exp_index = exp_index + Timedelta(1, 'h') - Timedelta(1, 'ns')
6971
tm.assert_index_equal(result.index, exp_index)
7072

7173
delta = timedelta(hours=23, minutes=59)
7274
result = df.to_timestamp('T', 'end')
7375
exp_index = _get_with_delta(delta)
76+
exp_index = exp_index + Timedelta(1, 'm') - Timedelta(1, 'ns')
7477
tm.assert_index_equal(result.index, exp_index)
7578

7679
result = df.to_timestamp('S', 'end')
7780
delta = timedelta(hours=23, minutes=59, seconds=59)
7881
exp_index = _get_with_delta(delta)
82+
exp_index = exp_index + Timedelta(1, 's') - Timedelta(1, 'ns')
7983
tm.assert_index_equal(result.index, exp_index)
8084

8185
# columns
8286
df = df.T
8387

8488
exp_index = date_range('1/1/2001', end='12/31/2009', freq='A-DEC')
89+
exp_index = exp_index + Timedelta(1, 'D') - Timedelta(1, 'ns')
8590
result = df.to_timestamp('D', 'end', axis=1)
8691
tm.assert_index_equal(result.columns, exp_index)
8792
tm.assert_numpy_array_equal(result.values, df.values)
@@ -93,16 +98,19 @@ def _get_with_delta(delta, freq='A-DEC'):
9398
delta = timedelta(hours=23)
9499
result = df.to_timestamp('H', 'end', axis=1)
95100
exp_index = _get_with_delta(delta)
101+
exp_index = exp_index + Timedelta(1, 'h') - Timedelta(1, 'ns')
96102
tm.assert_index_equal(result.columns, exp_index)
97103

98104
delta = timedelta(hours=23, minutes=59)
99105
result = df.to_timestamp('T', 'end', axis=1)
100106
exp_index = _get_with_delta(delta)
107+
exp_index = exp_index + Timedelta(1, 'm') - Timedelta(1, 'ns')
101108
tm.assert_index_equal(result.columns, exp_index)
102109

103110
result = df.to_timestamp('S', 'end', axis=1)
104111
delta = timedelta(hours=23, minutes=59, seconds=59)
105112
exp_index = _get_with_delta(delta)
113+
exp_index = exp_index + Timedelta(1, 's') - Timedelta(1, 'ns')
106114
tm.assert_index_equal(result.columns, exp_index)
107115

108116
# invalid axis

pandas/tests/indexes/period/test_period.py

+11
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,17 @@ def test_periods_number_check(self):
366366
with pytest.raises(ValueError):
367367
period_range('2011-1-1', '2012-1-1', 'B')
368368

369+
def test_start_time(self):
370+
index = PeriodIndex(freq='M', start='2016-01-01', end='2016-05-31')
371+
expected_index = date_range('2016-01-01', end='2016-05-31', freq='MS')
372+
tm.assert_index_equal(index.start_time, expected_index)
373+
374+
def test_end_time(self):
375+
index = PeriodIndex(freq='M', start='2016-01-01', end='2016-05-31')
376+
expected_index = date_range('2016-01-01', end='2016-05-31', freq='M')
377+
expected_index = expected_index.shift(1, freq='D').shift(-1, freq='ns')
378+
tm.assert_index_equal(index.end_time, expected_index)
379+
369380
def test_index_duplicate_periods(self):
370381
# monotonic
371382
idx = PeriodIndex([2000, 2007, 2007, 2009, 2009], freq='A-JUN')

pandas/tests/indexes/period/test_scalar_compat.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
22
"""Tests for PeriodIndex behaving like a vectorized Period scalar"""
33

4-
from pandas import PeriodIndex, date_range
4+
from pandas import PeriodIndex, date_range, Timedelta
55
import pandas.util.testing as tm
66

77

@@ -14,4 +14,5 @@ def test_start_time(self):
1414
def test_end_time(self):
1515
index = PeriodIndex(freq='M', start='2016-01-01', end='2016-05-31')
1616
expected_index = date_range('2016-01-01', end='2016-05-31', freq='M')
17+
expected_index = expected_index + Timedelta(1, 'D') - Timedelta(1, 'ns')
1718
tm.assert_index_equal(index.end_time, expected_index)

pandas/tests/indexes/period/test_tools.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pytest
44

55
import pandas as pd
6+
from pandas import Timedelta
67
import pandas.util.testing as tm
78
import pandas.core.indexes.period as period
89
from pandas.compat import lrange
@@ -60,7 +61,7 @@ def test_to_timestamp(self):
6061

6162
exp_index = date_range('1/1/2001', end='12/31/2009', freq='A-DEC')
6263
result = series.to_timestamp(how='end')
63-
exp_index = exp_index + pd.Timedelta(1, 'D') - pd.Timedelta(1, 'ns')
64+
exp_index = exp_index + Timedelta(1, 'D') - Timedelta(1, 'ns')
6465
tm.assert_index_equal(result.index, exp_index)
6566
assert result.name == 'foo'
6667

@@ -75,19 +76,19 @@ def _get_with_delta(delta, freq='A-DEC'):
7576
delta = timedelta(hours=23)
7677
result = series.to_timestamp('H', 'end')
7778
exp_index = _get_with_delta(delta)
78-
exp_index = exp_index + pd.Timedelta(1, 'h') - pd.Timedelta(1, 'ns')
79+
exp_index = exp_index + Timedelta(1, 'h') - Timedelta(1, 'ns')
7980
tm.assert_index_equal(result.index, exp_index)
8081

8182
delta = timedelta(hours=23, minutes=59)
8283
result = series.to_timestamp('T', 'end')
8384
exp_index = _get_with_delta(delta)
84-
exp_index = exp_index + pd.Timedelta(1, 'm') - pd.Timedelta(1, 'ns')
85+
exp_index = exp_index + Timedelta(1, 'm') - Timedelta(1, 'ns')
8586
tm.assert_index_equal(result.index, exp_index)
8687

8788
result = series.to_timestamp('S', 'end')
8889
delta = timedelta(hours=23, minutes=59, seconds=59)
8990
exp_index = _get_with_delta(delta)
90-
exp_index = exp_index + pd.Timedelta(1, 's') - pd.Timedelta(1, 'ns')
91+
exp_index = exp_index + Timedelta(1, 's') - Timedelta(1, 'ns')
9192
tm.assert_index_equal(result.index, exp_index)
9293

9394
index = PeriodIndex(freq='H', start='1/1/2001', end='1/2/2001')
@@ -96,7 +97,7 @@ def _get_with_delta(delta, freq='A-DEC'):
9697
exp_index = date_range('1/1/2001 00:59:59', end='1/2/2001 00:59:59',
9798
freq='H')
9899
result = series.to_timestamp(how='end')
99-
exp_index = exp_index + pd.Timedelta(1, 's') - pd.Timedelta(1, 'ns')
100+
exp_index = exp_index + Timedelta(1, 's') - Timedelta(1, 'ns')
100101
tm.assert_index_equal(result.index, exp_index)
101102
assert result.name == 'foo'
102103

@@ -289,6 +290,7 @@ def test_to_timestamp_pi_mult(self):
289290
result = idx.to_timestamp(how='E')
290291
expected = DatetimeIndex(['2011-02-28', 'NaT', '2011-03-31'],
291292
name='idx')
293+
expected = expected + Timedelta(1, 'D') - Timedelta(1, 'ns')
292294
tm.assert_index_equal(result, expected)
293295

294296
def test_to_timestamp_pi_combined(self):
@@ -303,11 +305,13 @@ def test_to_timestamp_pi_combined(self):
303305
expected = DatetimeIndex(['2011-01-02 00:59:59',
304306
'2011-01-03 01:59:59'],
305307
name='idx')
308+
expected = expected + Timedelta(1, 's') - Timedelta(1, 'ns')
306309
tm.assert_index_equal(result, expected)
307310

308311
result = idx.to_timestamp(how='E', freq='H')
309312
expected = DatetimeIndex(['2011-01-02 00:00', '2011-01-03 01:00'],
310313
name='idx')
314+
expected = expected + Timedelta(1, 'h') - Timedelta(1, 'ns')
311315
tm.assert_index_equal(result, expected)
312316

313317
def test_period_astype_to_timestamp(self):
@@ -317,6 +321,7 @@ def test_period_astype_to_timestamp(self):
317321
tm.assert_index_equal(pi.astype('datetime64[ns]'), exp)
318322

319323
exp = pd.DatetimeIndex(['2011-01-31', '2011-02-28', '2011-03-31'])
324+
exp = exp + Timedelta(1, 'D') - Timedelta(1, 'ns')
320325
tm.assert_index_equal(pi.astype('datetime64[ns]', how='end'), exp)
321326

322327
exp = pd.DatetimeIndex(['2011-01-01', '2011-02-01', '2011-03-01'],
@@ -326,6 +331,7 @@ def test_period_astype_to_timestamp(self):
326331

327332
exp = pd.DatetimeIndex(['2011-01-31', '2011-02-28', '2011-03-31'],
328333
tz='US/Eastern')
334+
exp = exp + Timedelta(1, 'D') - Timedelta(1, 'ns')
329335
res = pi.astype('datetime64[ns, US/Eastern]', how='end')
330336
tm.assert_index_equal(res, exp)
331337

pandas/tests/scalar/period/test_period.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from datetime import datetime, date, timedelta
66

77
import pandas as pd
8+
from pandas import Timedelta
89
import pandas.util.testing as tm
910
import pandas.core.indexes.period as period
1011
from pandas.compat import text_type, iteritems
@@ -274,12 +275,14 @@ def test_timestamp_tz_arg_dateutil_from_string(self):
274275

275276
def test_timestamp_mult(self):
276277
p = pd.Period('2011-01', freq='M')
277-
assert p.to_timestamp(how='S') == pd.Timestamp('2011-01-01')
278-
assert p.to_timestamp(how='E') == pd.Timestamp('2011-01-31')
278+
assert p.to_timestamp(how='S') == Timestamp('2011-01-01')
279+
expected = Timestamp('2011-02-01') - Timedelta(1, 'ns')
280+
assert p.to_timestamp(how='E') == expected
279281

280282
p = pd.Period('2011-01', freq='3M')
281-
assert p.to_timestamp(how='S') == pd.Timestamp('2011-01-01')
282-
assert p.to_timestamp(how='E') == pd.Timestamp('2011-03-31')
283+
assert p.to_timestamp(how='S') == Timestamp('2011-01-01')
284+
expected = Timestamp('2011-04-01') - Timedelta(1, 'ns')
285+
assert p.to_timestamp(how='E') == expected
283286

284287
def test_construction(self):
285288
i1 = Period('1/1/2005', freq='M')
@@ -611,19 +614,19 @@ def _ex(p):
611614
p = Period('1985', freq='A')
612615

613616
result = p.to_timestamp('H', how='end')
614-
expected = datetime(1985, 12, 31, 23)
617+
expected = Timestamp(1986, 1, 1) - Timedelta(1, 'ns')
615618
assert result == expected
616619
result = p.to_timestamp('3H', how='end')
617620
assert result == expected
618621

619622
result = p.to_timestamp('T', how='end')
620-
expected = datetime(1985, 12, 31, 23, 59)
623+
expected = Timestamp(1986, 1, 1) - Timedelta(1, 'ns')
621624
assert result == expected
622625
result = p.to_timestamp('2T', how='end')
623626
assert result == expected
624627

625628
result = p.to_timestamp(how='end')
626-
expected = datetime(1985, 12, 31)
629+
expected = Timestamp(1986, 1, 1) - Timedelta(1, 'ns')
627630
assert result == expected
628631

629632
expected = datetime(1985, 1, 1)

pandas/tests/test_resample.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import pandas.util.testing as tm
1717
import pandas.util._test_decorators as td
1818
from pandas import (Series, DataFrame, Panel, Index, isna,
19-
notna, Timestamp)
19+
notna, Timestamp, Timedelta)
2020

2121
from pandas.core.dtypes.generic import ABCSeries, ABCDataFrame
2222
from pandas.compat import range, lrange, zip, product, OrderedDict
@@ -1830,12 +1830,14 @@ def test_resample_anchored_intraday(self):
18301830
result = df.resample('M').mean()
18311831
expected = df.resample(
18321832
'M', kind='period').mean().to_timestamp(how='end')
1833+
expected.index += Timedelta(1, 'ns') - Timedelta(1, 'D')
18331834
tm.assert_frame_equal(result, expected)
18341835

18351836
result = df.resample('M', closed='left').mean()
18361837
exp = df.tshift(1, freq='D').resample('M', kind='period').mean()
18371838
exp = exp.to_timestamp(how='end')
18381839

1840+
exp.index = exp.index + Timedelta(1, 'ns') - Timedelta(1, 'D')
18391841
tm.assert_frame_equal(result, exp)
18401842

18411843
rng = date_range('1/1/2012', '4/1/2012', freq='100min')
@@ -1844,12 +1846,14 @@ def test_resample_anchored_intraday(self):
18441846
result = df.resample('Q').mean()
18451847
expected = df.resample(
18461848
'Q', kind='period').mean().to_timestamp(how='end')
1849+
expected.index += Timedelta(1, 'ns') - Timedelta(1, 'D')
18471850
tm.assert_frame_equal(result, expected)
18481851

18491852
result = df.resample('Q', closed='left').mean()
18501853
expected = df.tshift(1, freq='D').resample('Q', kind='period',
18511854
closed='left').mean()
18521855
expected = expected.to_timestamp(how='end')
1856+
expected.index += Timedelta(1, 'ns') - Timedelta(1, 'D')
18531857
tm.assert_frame_equal(result, expected)
18541858

18551859
ts = _simple_ts('2012-04-29 23:00', '2012-04-30 5:00', freq='h')
@@ -2580,7 +2584,7 @@ def test_resample_to_timestamps(self):
25802584
ts = _simple_pts('1/1/1990', '12/31/1995', freq='M')
25812585

25822586
result = ts.resample('A-DEC', kind='timestamp').mean()
2583-
expected = ts.to_timestamp(how='end').resample('A-DEC').mean()
2587+
expected = ts.to_timestamp(how='start').resample('A-DEC').mean()
25842588
assert_series_equal(result, expected)
25852589

25862590
def test_resample_to_quarterly(self):

0 commit comments

Comments
 (0)