Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting into date-time format in pandas?

I need help converting into python/pandas date time format. For example, my times are saved like the following line:

2017-01-01 05:30:24.468911+00:00
.....
2017-05-05 01:51:31.351718+00:00

and I want to know the simplest way to convert this into date time format for essentially performing operations with time (like what is the range in days of my dataset to split up my dataset into chunks by time, what's the time difference from one time to another)? I don't mind losing some of the significance for the times if that makes things easier. Thank you so much!

like image 922
Jane Sully Avatar asked Dec 01 '25 11:12

Jane Sully


1 Answers

Timestamp will convert it for you.

>>> pd.Timestamp('2017-01-01 05:30:24.468911+00:00')
Timestamp('2017-01-01 05:30:24.468911+0000', tz='UTC')

Let's say you have a dataframe that includes your timestamp column (let's call it stamp). You can use apply on that column together with Timestamp:

df = pd.DataFrame(
    {'stamp': ['2017-01-01 05:30:24.468911+00:00',
               '2017-05-05 01:51:31.351718+00:00']})
>>> df
    stamp
0   2017-01-01 05:30:24.468911+00:00
1   2017-05-05 01:51:31.351718+00:00

>>> df['stamp'].apply(pd.Timestamp)
0   2017-01-01 05:30:24.468911+00:00
1   2017-05-05 01:51:31.351718+00:00
Name: stamp, dtype: datetime64[ns, UTC]

You could also use Timeseries:

>>> pd.TimeSeries(df.stamp)
0    2017-01-01 05:30:24.468911+00:00
1    2017-05-05 01:51:31.351718+00:00
Name: stamp, dtype: object

Once you have a Timestamp object, it is pretty efficient to manipulate. You can just difference their values, for example.

You may also want to have a look at this SO answer which discusses timezone unaware values to aware.

like image 179
Alexander Avatar answered Dec 03 '25 01:12

Alexander



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!