-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
ENH: Add milliseconds
field support for pd.DateOffset
#43598
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
Changes from 25 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
62918ce
initial
isVoid a04bf64
docstrings
isVoid b61c14d
Remove unwanted change
isVoid c067d24
Add tests
isVoid d0814a8
add license
isVoid 8e96489
Add to whats new section
isVoid a02be79
restructure TestDateOffset and extend with more coverage over parameters
isVoid 255fe23
remove license
isVoid b5a42e6
remove stale comments
isVoid a5dc38e
Merge branch 'master' into add_dateoffset_milliseconds
isVoid 073c319
Merge branch 'master' into add_dateoffset_milliseconds
jreback 4801de3
Removing existing mul tests
isVoid dbadf9f
change test parameter name
isVoid 6a98391
Merge branch 'add_dateoffset_milliseconds' of https://github.com/isVo…
isVoid 30d3403
Add some docs for replace component usage
isVoid bafceb6
docstring and test fixes
isVoid 3bd154c
add assertion to constructed dateoffset
isVoid 4bb88df
Merge branch 'master' of https://github.com/pandas-dev/pandas into ad…
isVoid 41e537f
raise when millisecond (replacement form) is specified
isVoid 1da06d0
Merge branch 'master' into add_dateoffset_milliseconds
isVoid fcfce37
apply review comments
isVoid 1c7d7d7
fix fail tests
isVoid 99574ea
xfail instead of skip
isVoid b7eeb4d
Merge branch 'main' of git://github.com/pandas-dev/pandas into add_da…
isVoid e271461
Move notes to 1.5
isVoid 3a85b4e
Merge branch 'main' of git://github.com/pandas-dev/pandas into add_da…
isVoid b334f69
Add alternative to message
isVoid f9bfadd
Merge branch 'main' of git://github.com/pandas-dev/pandas into add_da…
isVoid 0fb51a1
Move replacement doc to docstring
isVoid 40f986a
Merge branch 'main' of github.com:pandas-dev/pandas into add_dateoffs…
isVoid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,6 +61,18 @@ | |
|
||
_ApplyCases = List[Tuple[BaseOffset, Dict[datetime, datetime]]] | ||
|
||
_ARITHMETIC_DATE_OFFSET = [ | ||
"years", | ||
"months", | ||
"weeks", | ||
"days", | ||
"hours", | ||
"minutes", | ||
"seconds", | ||
"milliseconds", | ||
"microseconds", | ||
] | ||
|
||
|
||
class TestCommon(Base): | ||
# executed value created by Base._get_offset | ||
|
@@ -551,28 +563,144 @@ def test_mul(self): | |
assert DateOffset(2) == 2 * DateOffset(1) | ||
assert DateOffset(2) == DateOffset(1) * 2 | ||
|
||
def test_constructor(self): | ||
|
||
assert (self.d + DateOffset(months=2)) == datetime(2008, 3, 2) | ||
assert (self.d - DateOffset(months=2)) == datetime(2007, 11, 2) | ||
@pytest.mark.parametrize("relativedelta_kwd", list(liboffsets._relativedelta_kwds)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to be Also, for consistency with elsewhere in this module, could maybe also change "relativedelta_kwd" -> "kwd"? |
||
def test_constructor(self, relativedelta_kwd, request): | ||
if relativedelta_kwd == "millisecond": | ||
request.node.add_marker( | ||
pytest.mark.xfail( | ||
raises=NotImplementedError, | ||
reason="Constructing DateOffset object with `millisecond` is not " | ||
"yet supported.", | ||
) | ||
) | ||
offset = DateOffset(**{relativedelta_kwd: 2}) | ||
assert offset.kwds == {relativedelta_kwd: 2} | ||
assert getattr(offset, relativedelta_kwd) == 2 | ||
|
||
def test_default_constructor(self): | ||
assert (self.d + DateOffset(2)) == datetime(2008, 1, 4) | ||
|
||
def test_is_anchored(self): | ||
assert not DateOffset(2).is_anchored() | ||
assert DateOffset(1).is_anchored() | ||
|
||
d = datetime(2008, 1, 31) | ||
assert (d + DateOffset(months=1)) == datetime(2008, 2, 29) | ||
|
||
def test_copy(self): | ||
assert DateOffset(months=2).copy() == DateOffset(months=2) | ||
assert DateOffset(milliseconds=1).copy() == DateOffset(milliseconds=1) | ||
|
||
@pytest.mark.parametrize( | ||
"arithmatic_offset_type, expected", | ||
zip( | ||
_ARITHMETIC_DATE_OFFSET, | ||
[ | ||
"2009-01-02", | ||
"2008-02-02", | ||
"2008-01-09", | ||
"2008-01-03", | ||
"2008-01-02 01:00:00", | ||
"2008-01-02 00:01:00", | ||
"2008-01-02 00:00:01", | ||
"2008-01-02 00:00:00.001000000", | ||
"2008-01-02 00:00:00.000001000", | ||
], | ||
), | ||
) | ||
def test_add(self, arithmatic_offset_type, expected): | ||
assert DateOffset(**{arithmatic_offset_type: 1}) + self.d == Timestamp(expected) | ||
assert self.d + DateOffset(**{arithmatic_offset_type: 1}) == Timestamp(expected) | ||
|
||
@pytest.mark.parametrize( | ||
"arithmatic_offset_type, expected", | ||
zip( | ||
_ARITHMETIC_DATE_OFFSET, | ||
[ | ||
"2007-01-02", | ||
"2007-12-02", | ||
"2007-12-26", | ||
"2008-01-01", | ||
"2008-01-01 23:00:00", | ||
"2008-01-01 23:59:00", | ||
"2008-01-01 23:59:59", | ||
"2008-01-01 23:59:59.999000000", | ||
"2008-01-01 23:59:59.999999000", | ||
], | ||
), | ||
) | ||
def test_sub(self, arithmatic_offset_type, expected): | ||
assert self.d - DateOffset(**{arithmatic_offset_type: 1}) == Timestamp(expected) | ||
with pytest.raises(TypeError, match="Cannot subtract datetime from offset"): | ||
DateOffset(**{arithmatic_offset_type: 1}) - self.d | ||
|
||
@pytest.mark.parametrize( | ||
"arithmatic_offset_type, n, expected", | ||
zip( | ||
_ARITHMETIC_DATE_OFFSET, | ||
range(1, 10), | ||
[ | ||
"2009-01-02", | ||
"2008-03-02", | ||
"2008-01-23", | ||
"2008-01-06", | ||
"2008-01-02 05:00:00", | ||
"2008-01-02 00:06:00", | ||
"2008-01-02 00:00:07", | ||
"2008-01-02 00:00:00.008000000", | ||
"2008-01-02 00:00:00.000009000", | ||
], | ||
), | ||
) | ||
def test_mul_add(self, arithmatic_offset_type, n, expected): | ||
assert DateOffset(**{arithmatic_offset_type: 1}) * n + self.d == Timestamp( | ||
expected | ||
) | ||
assert n * DateOffset(**{arithmatic_offset_type: 1}) + self.d == Timestamp( | ||
expected | ||
) | ||
assert self.d + DateOffset(**{arithmatic_offset_type: 1}) * n == Timestamp( | ||
expected | ||
) | ||
assert self.d + n * DateOffset(**{arithmatic_offset_type: 1}) == Timestamp( | ||
expected | ||
) | ||
|
||
@pytest.mark.parametrize( | ||
"arithmatic_offset_type, n, expected", | ||
zip( | ||
_ARITHMETIC_DATE_OFFSET, | ||
range(1, 10), | ||
[ | ||
"2007-01-02", | ||
"2007-11-02", | ||
"2007-12-12", | ||
"2007-12-29", | ||
"2008-01-01 19:00:00", | ||
"2008-01-01 23:54:00", | ||
"2008-01-01 23:59:53", | ||
"2008-01-01 23:59:59.992000000", | ||
"2008-01-01 23:59:59.999991000", | ||
], | ||
), | ||
) | ||
def test_mul_sub(self, arithmatic_offset_type, n, expected): | ||
assert self.d - DateOffset(**{arithmatic_offset_type: 1}) * n == Timestamp( | ||
expected | ||
) | ||
assert self.d - n * DateOffset(**{arithmatic_offset_type: 1}) == Timestamp( | ||
expected | ||
) | ||
|
||
def test_leap_year(self): | ||
d = datetime(2008, 1, 31) | ||
assert (d + DateOffset(months=1)) == datetime(2008, 2, 29) | ||
|
||
def test_eq(self): | ||
offset1 = DateOffset(days=1) | ||
offset2 = DateOffset(days=365) | ||
|
||
assert offset1 != offset2 | ||
|
||
assert DateOffset(milliseconds=3) != DateOffset(milliseconds=7) | ||
|
||
|
||
class TestOffsetNames: | ||
def test_get_offset_name(self): | ||
|
@@ -741,7 +869,15 @@ def test_month_offset_name(month_classes): | |
|
||
|
||
@pytest.mark.parametrize("kwd", sorted(liboffsets._relativedelta_kwds)) | ||
def test_valid_relativedelta_kwargs(kwd): | ||
def test_valid_relativedelta_kwargs(kwd, request): | ||
if kwd == "millisecond": | ||
request.node.add_marker( | ||
pytest.mark.xfail( | ||
raises=NotImplementedError, | ||
reason="Constructing DateOffset object with `millisecond` is not " | ||
"yet supported.", | ||
) | ||
) | ||
# Check that all the arguments specified in liboffsets._relativedelta_kwds | ||
# are in fact valid relativedelta keyword args | ||
DateOffset(**{kwd: 1}) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.