-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: Add isocalendar accessor to DatetimeIndex and Series.dt #33220
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
mroeschke
merged 17 commits into
pandas-dev:master
from
mgmarino:add-year-for-week-of-year
Apr 12, 2020
Merged
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
d2edb11
Add function to get_iso_calendar
mgmarino 7e807a6
Add fields function to build isocalendar array
mgmarino 3fd4cfb
Add isocalendar property to DatetimeArray
mgmarino 4a32f4a
Fix dt tests to handle DataFrame output
mgmarino e994788
Update what’s new for isocalendar
mgmarino 93d093e
Add columns explicitly when creating DataFrame
mgmarino 3f8d79b
Set fill value to a tuple of nan
mgmarino 37e9e44
Define return of iso_calendar as ctypedef
mgmarino 7865a70
Use to_datetime instead of dtype=“datetime64[D]”
mgmarino 83e3005
Fix doc string to be in row format
mgmarino f0d1ae6
Improve readability of ccalendar test
mgmarino ff7b7e6
Return Int64 dataframe when NaT present
mgmarino 330a103
Clean up example to use a single Series
mgmarino 9c30351
Always return an Int64 data frame
mgmarino f40d2f0
Return UInt32 per calendar ops standard
mgmarino aa6a193
Add timeseries documentation
mgmarino b495bfd
Merge branch 'master' into add-year-for-week-of-year
jreback 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
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 |
---|---|---|
|
@@ -182,7 +182,7 @@ class DatetimeArray(dtl.DatetimeLikeArrayMixin, dtl.TimelikeOps, dtl.DatelikeOps | |
"microsecond", | ||
"nanosecond", | ||
] | ||
_other_ops = ["date", "time", "timetz"] | ||
_other_ops = ["date", "time", "timetz", "isocalendar"] | ||
_datetimelike_ops = _field_ops + _object_ops + _bool_ops + _other_ops | ||
_datetimelike_methods = [ | ||
"to_period", | ||
|
@@ -1234,6 +1234,50 @@ def date(self): | |
|
||
return tslib.ints_to_pydatetime(timestamps, box="date") | ||
|
||
@property | ||
def isocalendar(self): | ||
""" | ||
Returns a DataFrame with the year, week, and day calculated according to | ||
the ISO 8601 standard. | ||
|
||
.. versionadded:: 1.1.0 | ||
|
||
Returns | ||
------- | ||
DataFrame | ||
with columns year, week and day | ||
|
||
See Also | ||
-------- | ||
Timestamp.isocalendar | ||
datetime.date.isocalendar | ||
|
||
Examples | ||
-------- | ||
>>> idx = pd.date_range(start='2019-12-29', freq='D', periods=4) | ||
>>> idx.isocalendar | ||
year week day | ||
0 2019 52 7 | ||
1 2020 1 1 | ||
2 2020 1 2 | ||
3 2020 1 3 | ||
>>> idx.isocalendar.week | ||
0 52 | ||
1 1 | ||
2 1 | ||
3 1 | ||
Name: week, dtype: Int64 | ||
""" | ||
from pandas import DataFrame | ||
|
||
sarray = fields.build_isocalendar_sarray(self.asi8) | ||
iso_calendar_df = DataFrame( | ||
sarray, columns=["year", "week", "day"], dtype="Int64" | ||
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. use UInt32 (as that's the default for all calendar component ops) |
||
) | ||
if self._hasnans: | ||
iso_calendar_df.iloc[self._isnan] = None | ||
return iso_calendar_df | ||
|
||
year = _field_accessor( | ||
"year", | ||
"Y", | ||
|
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 |
---|---|---|
|
@@ -219,6 +219,38 @@ def to_pydatetime(self) -> np.ndarray: | |
def freq(self): | ||
return self._get_values().inferred_freq | ||
|
||
@property | ||
def isocalendar(self): | ||
""" | ||
Returns a DataFrame with the year, week, and day calculated according to | ||
the ISO 8601 standard. | ||
|
||
.. versionadded:: 1.1.0 | ||
|
||
Returns | ||
------- | ||
DataFrame | ||
with columns year, week and day | ||
|
||
See Also | ||
-------- | ||
Timestamp.isocalendar | ||
datetime.date.isocalendar | ||
|
||
Examples | ||
-------- | ||
>>> ser = pd.to_datetime(pd.Series(["2010-01-01", pd.NaT])) | ||
>>> ser.dt.isocalendar | ||
year week day | ||
0 2009 53 5 | ||
1 <NA> <NA> <NA> | ||
>>> ser.dt.isocalendar.week | ||
0 53 | ||
1 <NA> | ||
Name: week, dtype: Int64 | ||
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. you will need to update this as per the dtype change |
||
""" | ||
return self._get_values().isocalendar.set_index(self._parent.index) | ||
|
||
|
||
@delegate_names( | ||
delegate=TimedeltaArray, accessors=TimedeltaArray._datetimelike_ops, typ="property" | ||
|
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make these return 0