feat: strftime function to output custom datetime strings - #244
Merged
Conversation
feat: `timezone` option added to `to_string` for local time outputs
Contributor
There was a problem hiding this comment.
Pull request overview
Adds additional string-formatting capabilities to the Timescale API, enabling timezone-aware string outputs via to_string and custom formatted outputs via a new strftime helper.
Changes:
- Extend
Timescale.to_string()with atimezoneoption for local/UTC/naive string rendering. - Introduce
Timescale.strftime(format, ...)for custom datetime string formats. - Adjust
Timescale.to_datetime()signature to accept additional keyword arguments.
Suppressed comments (2)
timescale/time.py:1040
to_stringno longer forwards**kwargstonp.datetime_as_string(they’re passed intoto_datetimeand then ignored), which is a behavior/API regression vs. the previous implementation. This breaks callers that relied on formatting kwargs likecasting=.
# convert to datetime objects
dtime = self.to_datetime(unit=unit, **kwargs)
return np.datetime_as_string(dtime, unit=unit, timezone=timezone)
timescale/time.py:1055
strftimeadvertises**kwargsas “datetime formatting” args, but they’re only (indirectly) used for conversion viato_datetime(and most keys are ignored). Since this is a new API, prefer an explicitunitparameter and ensure the datetime64 array is converted at microsecond resolution before casting todatetime.datetime(Python can’t represent nanoseconds).
def strftime(self, format: str, **kwargs):
"""
Convert a ``Timescale`` object to a custom formatted string array
Parameters
----------
format: str
formatting string for output string array
**kwargs: dict
keyword arguments for datetime formatting
"""
# convert to datetime objects
dtime = self.to_datetime(**kwargs).astype(datetime.datetime)
return np.array([d.strftime(format) for d in dtime])
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+1025
to
+1055
| def to_string(self, unit: str = "s", timezone="naive", **kwargs): | ||
| """ | ||
| Convert a ``Timescale`` object to a formatted string array | ||
|
|
||
| Parameters | ||
| ---------- | ||
| unit: str, default 's' | ||
| datetime unit for output string array | ||
| timezone: str, default 'naive' | ||
| timezone for output string array | ||
| **kwargs: dict | ||
| keyword arguments for datetime formatting | ||
| """ | ||
| return np.datetime_as_string( | ||
| self.to_datetime(unit=unit), unit=unit, **kwargs | ||
| ) | ||
| # convert to datetime objects | ||
| dtime = self.to_datetime(unit=unit, **kwargs) | ||
| return np.datetime_as_string(dtime, unit=unit, timezone=timezone) | ||
|
|
||
| def strftime(self, format: str, **kwargs): | ||
| """ | ||
| Convert a ``Timescale`` object to a custom formatted string array | ||
|
|
||
| Parameters | ||
| ---------- | ||
| format: str | ||
| formatting string for output string array | ||
| **kwargs: dict | ||
| keyword arguments for datetime formatting | ||
| """ | ||
| # convert to datetime objects | ||
| dtime = self.to_datetime(**kwargs).astype(datetime.datetime) | ||
| return np.array([d.strftime(format) for d in dtime]) |
| return scale * np.array(self.MJD - delta_time_epochs, dtype=np.float64) | ||
|
|
||
| def to_datetime(self, unit="ns"): | ||
| def to_datetime(self, unit="ns", **kwargs): |
Contributor
Contributor
Contributor
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
feat:
timezoneoption added toto_stringfor local time outputs