1
0
mirror of https://github.com/bspeice/metrik synced 2024-11-23 15:48:10 -05:00

assert Pandas + Timezones == Pain

This commit is contained in:
Bradlee Speice 2016-08-09 23:39:30 -04:00
parent 4dd3315d2c
commit 54ab89acff

View File

@ -17,17 +17,22 @@ class USDLibor(Task):
) )
def parse_london(dt_str): def parse_london(dt_str):
# Pandas does its best to try and help us out by modifying the # I'm getting inconsistent behavior in how Pandas parses the CSV
# actual csv content to try and add timezone and date information. # file for dates and times. On Travis, it doesn't look like the
# Which is not in any sense what we want. # content is being modified. On my computer, Pandas is spitting
# So we have convoluted steps to go and fix that. # back a localized time. So, after parsing, if we have a timezone-
# enabled datetime, switch to Europe/London, and if not, add the
# Europe/London info to it
london_tz = pytz.timezone('Europe/London') london_tz = pytz.timezone('Europe/London')
# Note that parse() implicitly adds timezone information because # Note that parse() implicitly adds timezone information because
# of how pandas gave us the value # of how pandas gave us the value
dt = parse(dt_str).replace(year=date.year, dt = parse(dt_str).replace(year=date.year,
month=date.month, month=date.month,
day=date.day) day=date.day)
try:
return dt.astimezone(london_tz) return dt.astimezone(london_tz)
except ValueError:
return london_tz.localize(dt)
# Skip 1 row at top for header (header=0), # Skip 1 row at top for header (header=0),
# and read 7 total rows. For whatever reason, # and read 7 total rows. For whatever reason,