Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert column of timestamp with a different UTC base to current UTC using Python

I have a dataframe with timestamps that are number of seconds since Jan 1,2010 midnight UTC time zone. I need to convert them to the present UTC time. I am able to do that for a given row using timedelta but not able to implement that for the entire timestamp column.

    # The base of my timestamp is UTC(2010 Jan 1, midnight)
    # able to do it for a specific delta i.e. df["timestamp][0]
    sec_shift = pd.Timedelta(seconds=201852000)
    new_time = datetime.datetime(2010,1,1)+sec_shift

    # how do i do this for the entire df["timestamp"] column?
    df = pd.DataFrame({"timestamp":[201852000,201852060,201852120,201852180,201852240], "B":[160863892,160864264,160864637,160865009,160865755]})
like image 211
schywalker Avatar asked Dec 08 '22 22:12

schywalker


1 Answers

try this:

dif = (datetime.datetime(2010,1,1) - datetime.datetime(1970,1,1)).total_seconds()
sec_shift = 4*60*60
pd.to_datetime(df.timestamp + diff + sec_shift, unit='s')

demo:

In [29]: pd.to_datetime(df.timestamp + dif + sec_shift, unit='s')
Out[29]:
0   2016-05-25 10:00:00
1   2016-05-25 10:01:00
2   2016-05-25 10:02:00
3   2016-05-25 10:03:00
4   2016-05-25 10:04:00
Name: timestamp, dtype: datetime64[ns]

PS i would recommend you to use standard solutions, for example to store number of seconds since Jan 1,1970 midnight UTC (Standard UNIX timestamp) - this will make your work bit simpler

like image 182
MaxU - stop WAR against UA Avatar answered May 10 '23 04:05

MaxU - stop WAR against UA