Converting Pandas DatetimeIndex to a numeric format

Nick Gao

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!

cs95

Convert to Timedelta and extract the total seconds from dt.total_seconds:

df

        date
0 2013-01-01
1 2013-01-02
2 2013-01-03
3 2013-01-04
4 2013-01-05
5 2013-01-06
6 2013-01-07
7 2013-01-08
8 2013-01-09
9 2013-01-10

pd.to_timedelta(df.date).dt.total_seconds()

0    1.356998e+09
1    1.357085e+09
2    1.357171e+09
3    1.357258e+09
4    1.357344e+09
5    1.357430e+09
6    1.357517e+09
7    1.357603e+09
8    1.357690e+09
9    1.357776e+09
Name: date, dtype: float64

Or, maybe, the data would be more useful presented as an int type:

pd.to_timedelta(df.date).dt.total_seconds().astype(int)

0    1356998400
1    1357084800
2    1357171200
3    1357257600
4    1357344000
5    1357430400
6    1357516800
7    1357603200
8    1357689600
9    1357776000
Name: date, dtype: int64

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Pandas: Converting to numeric, creating NaNs when necessary

Converting pandas DatetimeIndex to 'float days format' with Matplotlib.dates.datestr2num

Converting into date-time format in pandas?

Converting DatetimeIndex to Timedelta index

pandas format datetimeindex to quarters

Converting a text document with special format to Pandas DataFrame

pandas converting datetimeindex to timedeltaindex

Why does the format change when converting DatetimeIndex to np.array?

converting ddmmyy into mmyy format by using pandas?

SAS converting text to numeric F12.2 format error

Converting String to Numeric in pandas

how to avoid pandas in converting string to date format

Pandas not converting certain columns of dataframe to datetimeindex

Converting a pandas column to datetime with inconsistent format

Converting numeric time to datetime POSIXct format in R

Converting DateTime format of Excel into DateTime Pandas

Converting to date format in pandas

How to use AVG() function after converting a substring to a numeric format?

Pandas df converting timestamp column value format

Pandas column - converting string into specific date format

Converting data in byte format to Pandas Dataframe

After converting to numeric still not in numeric format in R

Formatting error on converting Pandas series to datetime format

Issue for converting a datetime format using pandas dataframe

Converting Data into pandas data frame format

Converting worded date format to datetime format in pandas

Converting maturities to numeric format

Converting numeric time (minutes or hours) to time format (HH:MM) in R

Converting numeric variable to Stata Date format