Note that there are some explanatory texts on larger screens.

plurals
  1. POConverting multiple columns with timestamp values to GMT time without using DateTimeIndex in pandas
    primarykey
    data
    text
    <p>I have a data frame with two columns with timestamps:</p> <pre><code>In [12]: df = pd.DataFrame({"start_time": range(1380805471, 1380805481), "end_time" : range(1380805481, 1380805491)}) In [13]: df.ix[:,['start_time','end_time']] Out[13]: start_time end_time 0 1380805471 1380805481 1 1380805472 1380805482 2 1380805473 1380805483 3 1380805474 1380805484 4 1380805475 1380805485 5 1380805476 1380805486 6 1380805477 1380805487 7 1380805478 1380805488 8 1380805479 1380805489 9 1380805480 1380805490 </code></pre> <p>The second step was just so that the start_time is displayed before end_time or else the columns are displayed alphabetically with only <code>df</code>.</p> <p>Now, I want to convert these timestamps into human readable times for display. Currently, I am doing::</p> <pre><code>In [15]: import datetime as dt In [16]: df['start_time'] = [dt.datetime.fromtimestamp(t) for t in df.start_time] In [17]: df['end_time'] = [dt.datetime.fromtimestamp(t) for t in df.end_time] In [18]: df.ix[:,['start_time','end_time']] Out[18]: start_time end_time 0 2013-10-03 18:34:31 2013-10-03 18:34:41 1 2013-10-03 18:34:32 2013-10-03 18:34:42 2 2013-10-03 18:34:33 2013-10-03 18:34:43 3 2013-10-03 18:34:34 2013-10-03 18:34:44 4 2013-10-03 18:34:35 2013-10-03 18:34:45 5 2013-10-03 18:34:36 2013-10-03 18:34:46 6 2013-10-03 18:34:37 2013-10-03 18:34:47 7 2013-10-03 18:34:38 2013-10-03 18:34:48 8 2013-10-03 18:34:39 2013-10-03 18:34:49 9 2013-10-03 18:34:40 2013-10-03 18:34:50 </code></pre> <p>My question - is there any pandas specific way of doing this which doesn't require using list comprehensions or is this the only way?</p> <p>The other way, I know of, for dealing with timestamps is using DateTimeIndex and then using localize methods to convert into the desired timezone. But this way requires that you make the column an index and thus, can be done for just one column. Please correct me if my understanding of DateTimeIndex is wrong. Also, I don't require these columns to be indexes.</p> <p>So is there any better way of doing this using pandas?</p> <p><strong>UPDATE</strong></p> <pre><code>In [52]: df['start_time'] = pd.DatetimeIndex(pd.to_datetime(df['start_time'],unit='s'),tz='UTC').tz_convert('Asia/Kolkata') In [53]: df Out[53]: end_time start_time 0 1380805481 2013-10-03 13:04:31 1 1380805482 2013-10-03 13:04:32 2 1380805483 2013-10-03 13:04:33 3 1380805484 2013-10-03 13:04:34 4 1380805485 2013-10-03 13:04:35 5 1380805486 2013-10-03 13:04:36 6 1380805487 2013-10-03 13:04:37 7 1380805488 2013-10-03 13:04:38 8 1380805489 2013-10-03 13:04:39 9 1380805490 2013-10-03 13:04:40 </code></pre> <p>Its still showing GMT time while I want GMT+5.30 even after specifying 'Asia/Kolkata' as the argument. Am I missing something?</p> <p>I need to convert both the columns to display the time in local timezone. The result of the DateTimeIndex is only visible if I set it as the index.</p> <pre><code>In [55]: t = pd.DatetimeIndex(pd.to_datetime(df['start_time'],unit='s'),tz='UTC').tz_convert('Asia/Kolkata') In [59]: df.set_index(t, inplace=True) In [60]: df Out[60]: end_time start_time 2013-10-03 18:34:31+05:30 1380805481 2013-10-03 13:04:31 2013-10-03 18:34:32+05:30 1380805482 2013-10-03 13:04:32 2013-10-03 18:34:33+05:30 1380805483 2013-10-03 13:04:33 2013-10-03 18:34:34+05:30 1380805484 2013-10-03 13:04:34 2013-10-03 18:34:35+05:30 1380805485 2013-10-03 13:04:35 2013-10-03 18:34:36+05:30 1380805486 2013-10-03 13:04:36 2013-10-03 18:34:37+05:30 1380805487 2013-10-03 13:04:37 2013-10-03 18:34:38+05:30 1380805488 2013-10-03 13:04:38 2013-10-03 18:34:39+05:30 1380805489 2013-10-03 13:04:39 2013-10-03 18:34:40+05:30 1380805490 2013-10-03 13:04:40 </code></pre> <p>How should I do it for both columns?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload