Created
November 8, 2018 15:22
-
-
Save liquidgenius/024a7aa77c71683b695b00f6274ac0c0 to your computer and use it in GitHub Desktop.
Generates a Pandas compatible Series of Timestamps using the Pendulum module. Addresses the issue where Pendulum Timestamps are not correctly converted to Pandas documented here: https://github.com/sdispater/pendulum/issues/246
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
import pendulum | |
import pandas as pd | |
def generate_pd_timestamp_series(start_year, | |
start_month, | |
start_day, | |
end_year, | |
end_month, | |
end_day, | |
time_zone, | |
interval, | |
units=1): | |
""" | |
Takes starting and ending dates and generates a Pandas friendly | |
Series of interval datetimes. Nanoseconds are not supported, | |
timezones are required. | |
Parameters | |
---------- | |
start_year (Integer): The starting year; 2018 or 1921, etc. | |
start_month (Integer): The starting month; 1-12. | |
start_day (Integer): The starting day of the month; 1-31. | |
end_year (Integer): The ending year; 2018 or 1921, etc. | |
end_month (Integer): The ending month; 1-12. | |
end_day (Integer): The ending day of the month; 1-31. | |
time_zone (String): The timezone for starting, ending and | |
resulting Series; "America/Denver", etc. | |
interval (String): The interval type; years, months, weeks, | |
days, hours, minutes, seconds. | |
units (Integer): The count of intervals between timestamps; 5, etc. | |
Returns | |
------- | |
result (Series): A pandas Series of datetimes between the | |
starting and ending dates in the designated interval units. | |
Example | |
------- | |
pd_series = generate_pd_timestamp_series(2018,1,1,2018,1,2,"America/Denver",'hours',units=8) | |
print(type(pd_series)) | |
> pandas.core.series.Series | |
print(pd_series) | |
> 0 2018-01-01 00:00:00 -0700 | |
> 1 2018-01-01 08:00:00 -0700 | |
> 2 2018-01-01 16:00:00 -0700 | |
> 3 2018-01-02 00:00:00 -0700 | |
> dtype: object | |
""" | |
start = pendulum.datetime(start_year, | |
start_month, | |
start_day).set(tz=time_zone) | |
end = pendulum.datetime(end_year, | |
end_month, | |
end_day).set(tz=time_zone) | |
period = pendulum.period(start, end) | |
return pd.Series([dt.strftime("%Y-%m-%d %H:%M:%S %z") for dt in period.range(interval,units)]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment