pandas 如何把时间转成index_将Pandas DatetimeIndex转换为数字格式

转换为Timedelta和dt.total_seconds提取总秒数:

data = \

{'date': {0: pd.Timestamp('2013-01-01 00:00:00'),

1: pd.Timestamp('2013-01-02 00:00:00'),

2: pd.Timestamp('2013-01-03 00:00:00'),

3: pd.Timestamp('2013-01-04 00:00:00'),

4: pd.Timestamp('2013-01-05 00:00:00'),

5: pd.Timestamp('2013-01-06 00:00:00'),

6: pd.Timestamp('2013-01-07 00:00:00'),

7: pd.Timestamp('2013-01-08 00:00:00'),

8: pd.Timestamp('2013-01-09 00:00:00'),

9: pd.Timestamp('2013-01-10 00:00:00')}}

df = pd.DataFrame.from_dict(data)

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

或者,也许,数据会更有用,如int类型:

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

你可能感兴趣的:(pandas,如何把时间转成index)