timezone conversion of a large list of timestamps from an excel file with python

2024/9/19 17:54:39

I have an excel file named "hello.xlsx". There is a column of timestamps that has a lot of rows (more than 80,000 rows for now). The file basically looks like this:

03/29/2018 19:24:50

03/29/2018 19:24:59

03/29/2018 19:24:59

03/29/2018 19:25:02

03/29/2018 19:25:06

03/29/2018 19:25:10

03/29/2018 19:25:20

03/29/2018 19:25:27

03/29/2018 19:25:27

03/29/2018 19:25:36

03/29/2018 19:25:49

And so on...

These timestamps are in UTC time, and I need to convert them to US Pacific Time (UTC, -7).

I searched online and tried to use some formulas within excel but failed to make it right. Then I wrote a piece of code as shown below:

df = pd.read_excel('hello1.xlsx', header=None)df[0] = pd.to_datetime(df[0]).dt.astimezone(timezone('US/Pacific'))df.to_excel('out.xlsx', index=False, header=False)

I tried running it but there appeared to be a problem. I think I need to change or add something to the second row of the code. I'm very new to python and I hope someone can help me figure it out I would really appreciate that. :)

Answer

In Excel (and in many other data software) time data are kept as decimals, which the integer part is one day and the floating part is the ratio of a day. So you may basically subtract 7/24 (which is 7 hours in Excel's time data format) in order to convert a value from UTC to UTC,-7

For instance when your time data is in A1, try writing below formula to A2:

=A1-(7/24)

Edit for the format:

In order to see the formulated cell as date/time, we should be changing its format accordingly. Below format would work for this case: enter image description here

https://en.xdnf.cn/q/119475.html

Related Q&A

N_gram frequency python NTLK

I want to write a function that returns the frequency of each element in the n-gram of a given text. Help please. I did this code fo counting frequency of 2-gramcode:from nltk import FreqDistfrom nltk.…

Is there a way to have a list of 4 billion numbers in Python?

I made a binary search function and Im curious what would happen if I used it on 4 billion numbers, but I get a MemoryError every time I use it. Is there a way to store the list without this issue?

ValueError: invalid literal for int() with base 10: when it worked before

Im having some issues with my program, basically what Im trying to do is Stenography, insert an image into another image and then extract the secret image.My program is able to insert just fine, but ex…

How to fetch the current branch from Jenkins?

I would like to query Jenkins using its API and Python to fetch the branch that is currently ready to be built.How can I do that?

How to vertically stretch graphs with matplotlib subplot [duplicate]

This question already has answers here:How do I change the size of figures drawn with Matplotlib?(16 answers)Closed 5 years ago.With the following code, I try to plot 12 different histograms in one pi…

Python Selenium Traceback (most recent call last):

Im trying to use selenium for a python web scraper but when I try to run the program I get the following error: "/Applications/Python 3.8/IDLE.app/Contents/MacOS/Python" "/Applications/P…

getting error while installing opencv via pip

python version = Python 3.8.0pip version = 19.3.1C:\Users\Sami Ullah Ch>pip3 install opencv-pythonERROR: Could not find a version that satisfies the requirement opencv-python (from versions: none)

Check whether text contains x numbers in a row [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 8 years ago.Improve…

How to add polling interval for a GET request in python

I have a case where I have to keep checking the response of a GET call until I see the status as success in the api response. And it takes around 20 to 50 mins to get the status from active to success.…