Skip to content

BUG: DatetimeIndex with nanosecond frequency does not include end #13762

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

Closed
wants to merge 1 commit into from
Closed
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
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