Skip to content

Commit

Permalink
BUG: DatetimeIndex with nanosecond frequency does not include end
Browse files Browse the repository at this point in the history
 - [x] closes #13672   - [x] tests added / passed   - [x] passes ``git
diff upstream/master | flake8 --diff``   - [x] whatsnew entry

Author: sinhrks <[email protected]>

Closes #13762 from sinhrks/date_range_nano and squashes the following commits:

0e63d70 [sinhrks] BUG: DatetimeIndex with nanosecond frequency does not include end
  • Loading branch information
sinhrks committed Jul 23, 2016
1 parent 9f94e6a commit e533947
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.19.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,7 @@ Bug Fixes
- Bug in invalid ``Timedelta`` arithmetic and comparison may raise ``ValueError`` rather than ``TypeError`` (:issue:`13624`)
- Bug in invalid datetime parsing in ``to_datetime`` and ``DatetimeIndex`` may raise ``TypeError`` rather than ``ValueError`` (:issue:`11169`, :issue:`11287`)
- Bug in ``Index`` created with tz-aware ``Timestamp`` and mismatched ``tz`` option incorrectly coerces timezone (:issue:`13692`)
- Bug in ``DatetimeIndex`` with nanosecond frequency does not include timestamp specified with ``end`` (:issue:`13672`)

- Bug in ``Categorical.remove_unused_categories()`` changes ``.codes`` dtype to platform int (:issue:`13261`)
- Bug in ``groupby`` with ``as_index=False`` returns all NaN's when grouping on multiple columns including a categorical one (:issue:`13204`)
Expand Down
3 changes: 2 additions & 1 deletion pandas/tseries/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -1964,7 +1964,8 @@ def _generate_regular_range(start, end, periods, offset):
b = Timestamp(start).value
# cannot just use e = Timestamp(end) + 1 because arange breaks when
# stride is too large, see GH10887
e = b + (Timestamp(end).value - b) // stride * stride + stride // 2
e = (b + (Timestamp(end).value - b) // stride * stride +
stride // 2 + 1)
# end.tz == start.tz by this point due to _generate implementation
tz = start.tz
elif start is not None:
Expand Down
9 changes: 9 additions & 0 deletions pandas/tseries/tests/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -1573,6 +1573,15 @@ def test_constructor_U(self):
self.assertRaises(ValueError, period_range, '2007-1-1', periods=500,
freq='X')

def test_constructor_nano(self):
idx = period_range(start=Period(ordinal=1, freq='N'),
end=Period(ordinal=4, freq='N'), freq='N')
exp = PeriodIndex([Period(ordinal=1, freq='N'),
Period(ordinal=2, freq='N'),
Period(ordinal=3, freq='N'),
Period(ordinal=4, freq='N')], freq='N')
tm.assert_index_equal(idx, exp)

def test_constructor_arrays_negative_year(self):
years = np.arange(1960, 2000, dtype=np.int64).repeat(4)
quarters = np.tile(np.array([1, 2, 3, 4], dtype=np.int64), 40)
Expand Down
65 changes: 65 additions & 0 deletions pandas/tseries/tests/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,71 @@ def test_to_datetime_freq(self):
self.assertEqual(xp.freq, rs.freq)
self.assertEqual(xp.tzinfo, rs.tzinfo)

def test_range_edges(self):
# GH 13672
idx = DatetimeIndex(start=Timestamp('1970-01-01 00:00:00.000000001'),
end=Timestamp('1970-01-01 00:00:00.000000004'),
freq='N')
exp = DatetimeIndex(['1970-01-01 00:00:00.000000001',
'1970-01-01 00:00:00.000000002',
'1970-01-01 00:00:00.000000003',
'1970-01-01 00:00:00.000000004'])
tm.assert_index_equal(idx, exp)

idx = DatetimeIndex(start=Timestamp('1970-01-01 00:00:00.000000004'),
end=Timestamp('1970-01-01 00:00:00.000000001'),
freq='N')
exp = DatetimeIndex([])
tm.assert_index_equal(idx, exp)

idx = DatetimeIndex(start=Timestamp('1970-01-01 00:00:00.000000001'),
end=Timestamp('1970-01-01 00:00:00.000000001'),
freq='N')
exp = DatetimeIndex(['1970-01-01 00:00:00.000000001'])
tm.assert_index_equal(idx, exp)

idx = DatetimeIndex(start=Timestamp('1970-01-01 00:00:00.000001'),
end=Timestamp('1970-01-01 00:00:00.000004'),
freq='U')
exp = DatetimeIndex(['1970-01-01 00:00:00.000001',
'1970-01-01 00:00:00.000002',
'1970-01-01 00:00:00.000003',
'1970-01-01 00:00:00.000004'])
tm.assert_index_equal(idx, exp)

idx = DatetimeIndex(start=Timestamp('1970-01-01 00:00:00.001'),
end=Timestamp('1970-01-01 00:00:00.004'),
freq='L')
exp = DatetimeIndex(['1970-01-01 00:00:00.001',
'1970-01-01 00:00:00.002',
'1970-01-01 00:00:00.003',
'1970-01-01 00:00:00.004'])
tm.assert_index_equal(idx, exp)

idx = DatetimeIndex(start=Timestamp('1970-01-01 00:00:01'),
end=Timestamp('1970-01-01 00:00:04'), freq='S')
exp = DatetimeIndex(['1970-01-01 00:00:01', '1970-01-01 00:00:02',
'1970-01-01 00:00:03', '1970-01-01 00:00:04'])
tm.assert_index_equal(idx, exp)

idx = DatetimeIndex(start=Timestamp('1970-01-01 00:01'),
end=Timestamp('1970-01-01 00:04'), freq='T')
exp = DatetimeIndex(['1970-01-01 00:01', '1970-01-01 00:02',
'1970-01-01 00:03', '1970-01-01 00:04'])
tm.assert_index_equal(idx, exp)

idx = DatetimeIndex(start=Timestamp('1970-01-01 01:00'),
end=Timestamp('1970-01-01 04:00'), freq='H')
exp = DatetimeIndex(['1970-01-01 01:00', '1970-01-01 02:00',
'1970-01-01 03:00', '1970-01-01 04:00'])
tm.assert_index_equal(idx, exp)

idx = DatetimeIndex(start=Timestamp('1970-01-01'),
end=Timestamp('1970-01-04'), freq='D')
exp = DatetimeIndex(['1970-01-01', '1970-01-02',
'1970-01-03', '1970-01-04'])
tm.assert_index_equal(idx, exp)

def test_range_misspecified(self):
# GH #1095

Expand Down

0 comments on commit e533947

Please sign in to comment.