Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Pandas DatetimeIndex to a numeric format

I want to convert the DatetimeIndex in my DataFrame to float format,which can be analysed in my model.Could someone tell me how to do it? Do I need to use date2num()function? Many thanks!

like image 314
Nick Gao Avatar asked Sep 30 '17 09:09

Nick Gao


People also ask

How do I convert data to numeric in pandas?

The best way to convert one or more columns of a DataFrame to numeric values is to use pandas.to_numeric() . This function will try to change non-numeric objects (such as strings) into integers or floating-point numbers as appropriate.

How do I convert a Timestamp to a number in Python?

Example 1: Integer timestamp of the current date and timeConvert the DateTime object into timestamp using DateTime. timestamp() method. We will get the timestamp in seconds. And then round off the timestamp and explicitly typecast the floating-point number into an integer to get the integer timestamp in seconds.

How do you convert DataFrame values to integers?

Use pandas DataFrame. astype() function to convert column to int (integer), you can apply this on a specific column or on an entire DataFrame. To cast the data type to 64-bit signed integer, you can use numpy. int64 , numpy.


1 Answers

I found another solution:

df['date'] = df['date'].astype('datetime64').astype(int).astype(float)
like image 108
Tomek Tajne Avatar answered Sep 22 '22 06:09

Tomek Tajne