pandas.Series.dt日期处理各种函数介绍

image.png

适用于dataframe的日期列

  • 如果是csv文件,识别date数据使用
df = pd.read_csv('df_weekgroup.csv',parse_dates = ['date'])
  • 年月日时分秒的数据转换成年月日 dt.to_perioddt.date
df['d'] = df['date'].dt.to_period('D')
或者
df['d1'] = df123.date.dt.date
  • 日期差dt.days
df123['datemin_justdate'] = df123['datemin'].dt.date
df123['date_justdate'] = df123['date'].dt.date
df123['daydiff'] = df123['date_justdate']- df123['datemin_justdate']
df123['weekgroup'] = (df123['daydiff'].dt.days)//7
  • 取年/月/日/时/分... dt.year
df['y'] = df.date.dt.year
  • 格式
rng = pd.date_range(pd.Timestamp("2018-03-10 09:00"),
                    periods=3, freq='s')
rng.strftime('%B %d, %Y, %r')
Index(['March 10, 2018, 09:00:00 AM', 'March 10, 2018, 09:00:01 AM',
       'March 10, 2018, 09:00:02 AM'],
      dtype='object')
  • 将时分秒转成0时刻dt.normalize()
idx = pd.date_range(start='2014-08-01 10:00', freq='H',
                    periods=3, tz='Asia/Calcutta')
idx
DatetimeIndex(['2014-08-01 10:00:00+05:30',
               '2014-08-01 11:00:00+05:30',
               '2014-08-01 12:00:00+05:30'],
                dtype='datetime64[ns, Asia/Calcutta]', freq='H')
idx.normalize()
DatetimeIndex(['2014-08-01 00:00:00+05:30',
               '2014-08-01 00:00:00+05:30',
               '2014-08-01 00:00:00+05:30'],
               dtype='datetime64[ns, Asia/Calcutta]', freq=None)

data['date']含有原来的日期格式 2015/10/12 ...等等

data[date2]=pd.to_datetime(data['date'])

转化为2015-05-01 00:00:00格式

data[date2]=pd.to_datetime(data['date']).dt.date

转化为2015-05-01格式

# %%
df2 = pd.DataFrame({'date':['2020-01-21 10:20:23', '2020-01-22 11:20:23', '2020-02-21 10:50:33']})

df2['date2']=pd.to_datetime(df2.date).dt.date
FastStoneEditor1 - Copy.jpg

你可能感兴趣的:(pandas.Series.dt日期处理各种函数介绍)