71

I have time from epochs timestamps I use data.Time_req = pd.to_datetime(data.Time_req) But I get UTC time, I need +5:30 from the given time. How do I tell pandas to use 'IST' timezone or just 5hrs 30 mins further to the time it currently shows me. eg. 7 hrs should become 12:30 hrs and so on.

pythonRcpp
  • 2,042
  • 6
  • 26
  • 48

4 Answers4

143

You can use tz_localize to set the timezone to UTC/+0000, and then tz_convert to add the timezone you want:

start = pd.to_datetime('2015-02-24')
rng = pd.date_range(start, periods=10)

df = pd.DataFrame({'Date': rng, 'a': range(10)})  

df.Date = df.Date.dt.tz_localize('UTC').dt.tz_convert('Asia/Kolkata')
print (df)
                       Date  a
0 2015-02-24 05:30:00+05:30  0
1 2015-02-25 05:30:00+05:30  1
2 2015-02-26 05:30:00+05:30  2
3 2015-02-27 05:30:00+05:30  3
4 2015-02-28 05:30:00+05:30  4
5 2015-03-01 05:30:00+05:30  5
6 2015-03-02 05:30:00+05:30  6
7 2015-03-03 05:30:00+05:30  7
8 2015-03-04 05:30:00+05:30  8
9 2015-03-05 05:30:00+05:30  9

Working with time zones.

If need add Timedelta only:

df.Date = df.Date + pd.Timedelta('05:30:00')
print (df)
                 Date  a
0 2015-02-24 05:30:00  0
1 2015-02-25 05:30:00  1
2 2015-02-26 05:30:00  2
3 2015-02-27 05:30:00  3
4 2015-02-28 05:30:00  4
5 2015-03-01 05:30:00  5
6 2015-03-02 05:30:00  6
7 2015-03-03 05:30:00  7
8 2015-03-04 05:30:00  8
9 2015-03-05 05:30:00  9

NOTE: Adding Timedelta will change the epoch timestamp associated with the datetime object. This may not be desired for many applications.

arturomp
  • 28,790
  • 10
  • 43
  • 72
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
6

From unix epoch timestamps

timestamps = [1656285319, 1656285336, 1656285424]

pd.to_datetime(timestamps, unit='s', utc=True).map(lambda x: x.tz_convert('Asia/Kolkata'))

outputs:

DatetimeIndex(['2022-06-27 04:45:19+05:30', '2022-06-27 04:45:36+05:30',
               '2022-06-27 04:47:04+05:30'],
              dtype='datetime64[ns, Asia/Kolkata]', freq=None)

All timezones

import pytz
pytz.all_timezones

full list here

imbr
  • 6,226
  • 4
  • 53
  • 65
3

The following worked for me:

df['Local_Time'] = pd.to_datetime(df,unit='s', utc=True)\
                      .map(lambda x: x.tz_convert('America/Chicago'))

This is the list of the name of timezones in pytz.

Hope this helps someone!

user3503711
  • 1,623
  • 1
  • 21
  • 32
1

These .map suggestions resulted in pandas to_datetime 'Timestamp' object has no attribute 'map' errors.

What worked in my case is:

df.insert(0, 'uploadtimestamp', pd.to_datetime('now').replace(microsecond=0))

df['uploadtimestamp'] = df['uploadtimestamp'].dt.tz_localize('UTC').dt.tz_convert('US/Eastern')

or

df.insert(0, 'uploadtimestamp',pd.to_datetime('now').replace(microsecond=0))

df['uploadtimestamp'] = df['uploadtimestamp'].dt.tz_localize('UTC').dt.tz_convert('US/Eastern').apply(lambda x: x.strftime('%Y-%m-%d %H:%M:%S'))

if want the timestamp column to be a string.

Ran A
  • 746
  • 3
  • 7
  • 19
M. Wismer
  • 11
  • 3