Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python Add datetime column in dataframe from an existing column

I have data in csv file as below

v,vw,o,c,h,l,t,n
18043.0,374.411,374.69,374.99,374.99,373.8,1656662400000,157
12003.0,375.6296,375.15,375.84,375.9,374.95,1656663300000,98
18426.0,376.0636,375.98,376.02,376.29,375.63,1656664200000,88
4700.0,376.0772,375.88,376.11,376.34,375.85,1656665100000,43
27969.0,376.5703,376.11,376.56,376.92,375.82,1656666000000,135
17922.0,376.7123,376.69,376.48,376.89,376.46,1656666900000,95
11805.0,376.5813,376.6,376.38,376.71,376.38,1656667800000,74
19888.0,376.9877,376.28,377.11,377.2,376.28,1656668700000,100
7853.0,376.7016,377.25,376.66,377.25,376.48,1656669600000,67
36560.0,377.3454,376.69,377.05,377.8,376.69,1656670500000,175
10862.0,376.354,376.74,376.06,376.74,376.06,1656671400000,92
14740.0,375.8719,376.09,375.74,376.09,375.71,1656672300000,126
78885.0,375.9584,375.88,375.901,376.14,375.71,1656673200000,628
43363.0,376.0552,375.8,376.31,376.4,375.68,1656674100000,277
...

where column t is having UTC unix timestamp in miliseconds (1/1000). I would like to add a column to the dataframe with timestamp in EST. Is there a way to achieve the same without going through each row? I tried few things but I am getting error similar to

TypeError: cannot convert the series to <class ‘int’>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Convert the column to datetime, then convert the timezone from UTC to EST:

df["t"] = pd.to_datetime(df["t"], unit="ms")
df["t"] = df["t"].dt.tz_localize("UTC").dt.tz_convert("US/Eastern")
          v        vw       o        c       h       l                         t    n
0   18043.0  374.4110  374.69  374.990  374.99  373.80 2022-07-01 04:00:00-04:00  157
1   12003.0  375.6296  375.15  375.840  375.90  374.95 2022-07-01 04:15:00-04:00   98
2   18426.0  376.0636  375.98  376.020  376.29  375.63 2022-07-01 04:30:00-04:00   88
3    4700.0  376.0772  375.88  376.110  376.34  375.85 2022-07-01 04:45:00-04:00   43
4   27969.0  376.5703  376.11  376.560  376.92  375.82 2022-07-01 05:00:00-04:00  135
5   17922.0  376.7123  376.69  376.480  376.89  376.46 2022-07-01 05:15:00-04:00   95
6   11805.0  376.5813  376.60  376.380  376.71  376.38 2022-07-01 05:30:00-04:00   74
7   19888.0  376.9877  376.28  377.110  377.20  376.28 2022-07-01 05:45:00-04:00  100
8    7853.0  376.7016  377.25  376.660  377.25  376.48 2022-07-01 06:00:00-04:00   67
9   36560.0  377.3454  376.69  377.050  377.80  376.69 2022-07-01 06:15:00-04:00  175
10  10862.0  376.3540  376.74  376.060  376.74  376.06 2022-07-01 06:30:00-04:00   92
11  14740.0  375.8719  376.09  375.740  376.09  375.71 2022-07-01 06:45:00-04:00  126
12  78885.0  375.9584  375.88  375.901  376.14  375.71 2022-07-01 07:00:00-04:00  628
13  43363.0  376.0552  375.80  376.310  376.40  375.68 2022-07-01 07:15:00-04:00  277
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading