pandas datetime时间处理一学就会

文章介绍了Pandas中处理日期和时间的数据类型,如Timestamp、datetime系列的创建与转换,包括使用`to_datetime`函数,日期范围生成器`date_range`,以及时间序列的重采样(resample)和滚动操作。还涉及到Timedelta的生成与计算,以及日期偏移量(Offset)的概念和应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Overview

DefinitionElement typeSeries typePandas data type
Date timesTimestampDatetimeIndexdatetime64[ns]
Time deltasTimedeltaTimedeltaIndextimedelta64[ns]
Time spansPeriodPeriodIndexperiod[freq]
Date offsetsDateOffsetNoneNone

Date times

Timestamp

pd.Timestamp(year, month, day, hour, minute, second)
pd.Timestamp(ts_input)

Keyword arguments year, month, day, hour, minute, second,
used to determine a timestamp.
ts_input is a time str.

Instance attributes

Attributes year, month, day, hour, minute, second,
get the time.
value, the num of nanoseconds
from zero on January 1, 1970(Computer Year)
to the given timestamp.

Class attributes

max, min, the max and min timestamp.

Datetime series

to_datetime

pd.to_datetime(arg, formatstr, )

arg, int, float, str, datetime, list, tuple, 1-d array, Series, DataFrame/dict-like
The object to convert to a datetime.
If a DataFrame is provided, the method expects minimally the following columns: “year”, “month”, “day”.

errors, {‘ignore’, ‘raise’, ‘coerce’}, default ‘raise’.
For a invalid parsing,

  • ‘raise’, raise an exception.
  • ‘coerce’, set as NaT.
  • ‘ignore’, return the input.

formatstr, default None. The strftime to parse time.
Refer to strftime documentation.

Range

pd.date_range(start=None, end=None, 
              periods=None, freq=None,
              tz=None, normalize=False,
              name=None, inclusive='both',
              *, 
              unit=None, **kwargs)

freq is the frequence, auto specified as ‘D’.
tz is time zone.
normalize, bool, default False,
whether normalize start/end dates to midnight.
inclusive, {‘both’, ‘neither’, ‘left’, ‘right’}, default ‘both’,
whether to set each bound as closed or open.

bdate_range, used to gen a series without holidays.

pd.bdate_range(start=None, end=None,
               periods=None, freq='B',
               tz=None, normalize=True,
               name=None,
               weekmask=None, holidays=None,
               inclusive='both',
               **kwargs)

freq, must specified.
weekmask, str,
mask of valid business days,
passed to numpy.busdaycalendar
auto specified as ‘Mon Tue Wed Thu Fri’.
holidays, list-like,
dates to exclude from the set of valid business days,
passed to numpy.busdaycalendar.

Change freq

s.asfreq(freq, 
         method=None, how=None,
         normalize=False,
         fill_value=None)

method, {‘backfill’/‘bfill’, ‘pad’/‘ffill’}, default None
how, {‘start’, ‘end’}, default end, for PeriodIndex only.
fill_value, scalar,
value to use for missing values, applied during upsampling.

dt object

s.dt

Normal attributes,
date, time, year, month, day, hour, minute, second, microsecond, nanosecond, dayofweek, dayofyear, weekofyear, daysinmonth, quarter

Returning a bool value,

s.dt.is_year_start
s.dt.is_quarter_start
s.dt.month_start

s.dt.is_year_end
s.dt.is_quarter_end
s.dt.is_month_end

Round,

s.dt.round(freq)
s.dt.ceil(freq)
s.dt.floor(freq)

Timedelta

Generate

Generate a pd.Timedelta,

pd.Timedelta(days, months, ...)
pd.Timedelta(time_str)

timestamp1 - timestamp2 # return a pd.Timedelta

Generate a pd.Series,

pd.to_timedelta(s)
pd.timedelta_range(start, end, freq, period)

Calculation

timestamp1 - timestamp2 # return a pd.Timedelta
timestamp + timedelta # return a pd.Timestamp

Offset

Offset methods

We could check all offset methods, using

pd.offsets.__all__

There are some usual offset methods.

pd.offsets.Day()
pd.offsets.BDay()
pd.offsets.CDay()
pd.offsets.WeekofMonth()
pd.offsets.MonthBegin()
pd.offsets.MonthEnd()

Offset strings

We could use offset strings to denote offsets as parameter freq.

StringOffset
‘MS’MonthBegin()
'M"MonthEnd()
‘B’BDay()
‘W-MON’CDay(weekmask=‘Mon’)
‘WOM-1MON’WeekOfMonth(week=0,weekday=0)

More refer to Offset aliases.

Date rolling and resample

Rolling

When the data type of index is datetime64[ns], we could use date rolling.

s.rolling(time)

It has same attributes and methods with normal rolling.

Shift

A slide method of series with time index.

s.shift(freq)

Resample

It is same with groupby, specified to datetime series.

s.resample(rule, origin)

rule, DateOffset, Timedelta or str,
the offset string or object representing target conversion.
origin, {‘epoch’, ‘start’, ‘start_day’},
Timestamp or str, default ‘start_day’.

  • ‘epoch’: 1970-01-01
  • ‘start’: the first value of the timeseries
  • ‘start_day’: the first day at midnight of the timeseries
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值