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
  Check time within specific time ranges basvdm 3 580 Jan-20-2025, 05:10 PM
Last Post: Gribouillis
Question [SOLVED] [datetime.strptime] ValueError: time data 'foo' does not match format 'bar' Winfried 1 1,166 Jan-02-2025, 02:09 AM
Last Post: lyly19
  Help about a specific time selection QJZ 0 387 Dec-01-2024, 11:25 AM
Last Post: QJZ
  How to move an object over time rather than instantly? temlotresid6 3 1,564 Oct-23-2024, 11:20 AM
Last Post: temlotresid6
  Is there a difference between Python’s time.sleep and win32api.Sleep? phpjunkie 4 1,037 Sep-21-2024, 05:17 PM
Last Post: aakritiintelligence
  How to insert text time - into video frames? oxidian 0 1,035 Aug-25-2024, 04:51 PM
Last Post: oxidian
  Using RTC time in Rasberry Pi Programs sab201 1 845 Aug-18-2024, 05:50 PM
Last Post: snippsat
Bug Python 3.12 cannot import local python files as modules sunflowerdog 25 17,597 Jul-31-2024, 01:52 PM
Last Post: sunflowerdog
  Schedule exit a program at a specific time 4 am every day. chubbychub 3 1,398 May-17-2024, 03:45 PM
Last Post: chubbychub
  Filer and sort files by modification time in a directory tester_V 5 2,260 May-02-2024, 05:39 PM
Last Post: tester_V

Forum Jump:

User Panel Messages

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