如何在DataFrame中的时间序列中将月份名称转换为月份编号?

时间:2019-05-27 14:23:54

标签: python-3.x pandas time-series

我在DataFrame中有一个列,其时间序列数据为'01 -Jun-2018'。 我还需要将所有其他月份的日期都转换为格式“ 01-06-2018”。 我该如何使用熊猫甚至不使用熊猫来做到这一点?

1 个答案:

答案 0 :(得分:2)

首先将列转换为日期时间,然后为自定义格式的字符串添加Series.dt.strftime-import pandas as pd import numpy as np np.random.seed(1) amountOfDataPoints = 12 myRange1 = pd.date_range('2018-04-09', periods=amountOfDataPoints, freq='1min') myRange2 = pd.date_range('2018-04-09', periods=amountOfDataPoints, freq='1h') data = np.random.rand(amountOfDataPoints) df1 = pd.DataFrame(data, myRange1) df2 = pd.DataFrame(data, myRange2) df1Resampled = df1.resample('10min', label='right').agg({ 'high': 'max', 'highDate': 'idxmax', 'low': 'min', 'lowDate': 'idxmin' }) df2Resampled = df2.resample('10min', label='right').agg({ 'high': 'max', 'highDate': 'idxmax', 'low': 'min', 'lowDate': 'idxmin' }) df1Resampled.head() Out[3]: high highDate low lowDate 0 0 0 0 2018-04-09 00:10:00 0.720324 2018-04-09 00:01:00 0.000114 2018-04-09 00:02:00 2018-04-09 00:20:00 0.685220 2018-04-09 00:11:00 0.419195 2018-04-09 00:10:00 df2Resampled.head() Out[4]: high low 0 0 2018-04-09 00:10:00 0.417022 0.417022 2018-04-09 00:20:00 NaN NaN 2018-04-09 00:30:00 NaN NaN 2018-04-09 00:40:00 NaN NaN 2018-04-09 00:50:00 NaN NaN

DD-MM-YYYY

或者:

df['Date'] = pd.to_datetime(df['Date']).dt.strftime('%d-%m-%Y')

如果需要日期时间,仅省略df['Date'] = pd.to_datetime(df['Date'], format='%d-%b-%Y').dt.strftime('%d-%m-%Y') ,但默认格式为.dt.strftime