Python Forum
How to change UTC time to local time in Python DataFrame?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to change UTC time to local time in Python DataFrame?
#1
Help convert UTC time to local. Here is a getter function that outputs the time in UTC in columns.

 def getminutedata(symbol, interval, lookback):
     frame = pd.DataFrame(client.get_historical_klines(symbol, interval, lookback + 'min ago UTC'))
     frame = frame.iloc[:,:6]
     frame.columns = ['Time', 'Open', 'High', 'Low', 'Close', 'Volume']
     frame = frame.set_index('Time')
     frame.index = pd.to_datetime(frame.index, unit='ms')
     frame = frame.astype(float)
     return frame
I've tried to change time in three ways, using pandas, dateutil and pytz, but nothing worked, not enough knowledge in Python. Knowledgeable people help write down the necessary lines of code to change time. Thank you in advance. Huh Huh
Reply
#2
@snippsat posted some good info on this topic here: https://python-forum.io/thread-37473-pos...#pid158374

... which may be of help to you.

It's not an easy subject.
SamKnight likes this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
I think many webhosts set their servers to UTC (a French abbreviation, I believe).

Chinese time is 8 hours or 28800 seconds behind UTC, so if the webhost is on UTC, I just subtract 28800 if I want the Chinese time.

My current webhost runs on Chinese local time, which makes things easier for me, being in China.

But I still need to change opening and closing times for classwork webpages. I know the time the first class starts, then just add on the timedelta for the other classes in a loop using the class names.

Look at the docs here.

from datetime import datetime, timedelta
# time the first class starts
my_string = 'September 13 2021 09:50:00'
my_date = datetime.strptime(my_string, "%B %d %Y %H:%M:%S")
print(my_date)
# set the required timedelta
BE1_start = timedelta(days=+2, hours=+5, minutes=+55)
new_date = my_date + BE1_start
print(new_date)
Once you know your timedelta, just make a function to add or subtract the correct amount of time from the UTC time.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Date Time Series Help...Please spra8560 2 352 Feb-01-2024, 01:38 PM
Last Post: spra8560
  Python date format changes to date & time 1418 4 587 Jan-20-2024, 04:45 AM
Last Post: 1418
  time difference bettwenn logs enkliy 14 966 Nov-21-2023, 04:51 PM
Last Post: rob101
Question Need Help with Vehicle Routing Problem with Time Windows (VRPTW) in Python kasper321421312 1 554 Nov-10-2023, 08:19 PM
Last Post: snippsat
  How do I stream and record at the same time with arducam? traderjoe 0 456 Oct-23-2023, 12:01 AM
Last Post: traderjoe
  i tried to install python for the first time today and pretty certain im being remote brianlj 2 540 Oct-03-2023, 11:15 AM
Last Post: snippsat
  [Python 2.7] Why can't I press [ESC] a fourth time? Ashwood 3 656 Aug-27-2023, 02:01 PM
Last Post: deanhystad
  Hard time trying to figure out the difference between two strings carecavoador 2 675 Aug-16-2023, 04:53 PM
Last Post: carecavoador
  return next item each time a function is executed User3000 19 2,277 Aug-06-2023, 02:29 PM
Last Post: deanhystad
  Downloading time zone aware files, getting wrong files(by date))s tester_V 9 1,017 Jul-23-2023, 08:32 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020