I have a data set like the following:
- start_time_UTC
- 2021-09-16T12:00:00-05:00
- 2021-09-15T19:00:00-05:00
- 2021-09-16T08:18:00-05:00
- 2021-09-16T12:22:10-05:00
My default time is UTC, but I would like to create multiple columns based of the start_time_UTC to create cst, mst, and est.
- from domomagic import *
- from datetime import datetime as dt
- import pandas as pd
- from pytz import timezone
- import pytz
-
- df = read_dataframe('my_dataset')
-
- df['time_stamp'] = df['start_time']
- utc = timezone('UTC')
- cst = timezone('US/Central')
- mst = timezone('US/Mountain')
- est = timezone('US/Eastern')
-
- # my issue begins at 'published_time"
- published_time = time_stamp.apply(lambda x: dt.strptime(df.time_stamp, '%a, %d %b %Y %H:%M:%S %Z'))
- time_utc = published_time.replace(tzinfo=utc)
- time_cst = published_time.replace(tzinfo=cst)
- time_mst = published_time.replace(tzinfo=mst)
- time_est = published_time.replace(tzinfo=est)
-
- # then call using time_timezone
- df['time_published_cst'] = time_cst.strftime('%I:%M:%S %p %Z')
- df['time_published_est'] = time_est.strftime('%I:%M:%S %p %Z')
- df['time_published_mst'] = time_mst.strftime('%I:%M:%S %p %Z')
- df['time_published_utc'] = time_utc.strftime('%I:%M:%S %p %Z')
Initially I received an error for the following that said, "TypeError: strptime() argument 1 must be str, not Series":
- published_time = datetime.strptime(time_stamp, '%a, %d %b %Y %H:%M:%S %Z')
So, I changed it using lambda but got an error saying, " NameError: name 'time_stamp' is not defined"
- published_time = time_stamp.apply(lambda x: dt.strptime(df.time_stamp, '%a, %d %b %Y %H:%M:%S %Z'))
Then again I tried the following but got an error that said, 'time_stamp' does not match format '%a, %d %b %Y %H:%M:%S %Z'
- published_time = df['time_stamp'].apply(lambda x: dt.strptime('time_stamp', '%a, %d %b %Y %H:%M:%S %Z'))
Can anyone tell me what I could be wrong?