Jovian
⭐️
Sign In
In [3]:
from xplan.pipe import *

datetime 格式定义

代码 说明
%Y 4位数的年
%y 2位数的年
%m 2位数的月[01,12]
%d 2位数的日[01,31]
%H 时(24小时制)[00,23]
%l 时(12小时制)[01,12]
%M 2位数的分[00,59]
%S 秒[00,61]有闰秒的存在
%w 用整数表示的星期几[0(星期天),6]
%F %Y-%m-%d简写形式例如,2017-06-27
%D %m/%d/%y简写形式

创建 ts

  • pd.timedelta_range
  • pd.date_range
In [109]:
pd.date_range(start='1/1/2018', periods=2, freq='s') # S
pd.date_range(start='1/1/2018', periods=2, freq='min') # Min
pd.date_range(start='2018-01-01', periods=2, freq='1h') # H
pd.date_range(start='2018-01-01', periods=2, freq='2d') # D
pd.date_range(start='2018-01-01', periods=2, freq='3w') # D
pd.date_range(start='2018-01-01', periods=2, freq='4m') # M
pd.date_range(start='2018-01-01', periods=2, freq='5y') # Y
pd.date_range(start=datetime.datetime.today(), periods=2, freq='d')
Out[109]:
DatetimeIndex(['2018-01-01 00:00:00', '2018-01-01 00:00:01'], dtype='datetime64[ns]', freq='S')
Out[109]:
DatetimeIndex(['2018-01-01 00:00:00', '2018-01-01 00:01:00'], dtype='datetime64[ns]', freq='T')
Out[109]:
DatetimeIndex(['2018-01-01 00:00:00', '2018-01-01 01:00:00'], dtype='datetime64[ns]', freq='H')
Out[109]:
DatetimeIndex(['2018-01-01', '2018-01-03'], dtype='datetime64[ns]', freq='2D')
Out[109]:
DatetimeIndex(['2018-01-07', '2018-01-28'], dtype='datetime64[ns]', freq='3W-SUN')
Out[109]:
DatetimeIndex(['2018-01-31', '2018-05-31'], dtype='datetime64[ns]', freq='4M')
Out[109]:
DatetimeIndex(['2018-12-31', '2023-12-31'], dtype='datetime64[ns]', freq='5A-DEC')
Out[109]:
DatetimeIndex(['2019-03-12 12:14:55.370025', '2019-03-13 12:14:55.370025'], dtype='datetime64[ns]', freq='D')

pd.Timestamp

  • pd.Timestamp('2019-03-12')
  • pd.Timestamp('2019/03/12')
  • pd.Timestamp('03/12/2019')
  • pd.Timestamp(pd.datetime.today())
  • pd.to_datetime(pd.datetime.today())

pd.Timedelta

  • pd.to_timedelta('1d') # pd.Timedelta

其他

pd.datetime  # datetime.datetime

pd.TimeGrouper
pd.TimedeltaIndex
pd.DateOffset
pd.DatetimeIndex
In [172]:
t = pd.datetime.now().timestamp()
ts = pd.Series([t] * 10, name='t')

# 时间戳 转 时间字符串
def timestamp2str(timestamp, format='%Y-%m-%d %H:%M:%S'):
    _ = time.localtime(timestamp)
    return time.strftime(format, _)
In [173]:
# 时间戳 转 时间字符串
ts = ts.map(timestamp2str)
ts
Out[173]:
0    2019-03-12 15:30:28
1    2019-03-12 15:30:28
2    2019-03-12 15:30:28
3    2019-03-12 15:30:28
4    2019-03-12 15:30:28
5    2019-03-12 15:30:28
6    2019-03-12 15:30:28
7    2019-03-12 15:30:28
8    2019-03-12 15:30:28
9    2019-03-12 15:30:28
Name: t, dtype: object
In [174]:
# 时间字符串 转 时间
ts = pd.to_datetime(ts, errors='coerce', infer_datetime_format=True)
ts
Out[174]:
0   2019-03-12 15:30:28
1   2019-03-12 15:30:28
2   2019-03-12 15:30:28
3   2019-03-12 15:30:28
4   2019-03-12 15:30:28
5   2019-03-12 15:30:28
6   2019-03-12 15:30:28
7   2019-03-12 15:30:28
8   2019-03-12 15:30:28
9   2019-03-12 15:30:28
Name: t, dtype: datetime64[ns]
In [175]:
# 时间 转 时间戳
ts.map(lambda x: x.timestamp())
Out[175]:
0    1.552405e+09
1    1.552405e+09
2    1.552405e+09
3    1.552405e+09
4    1.552405e+09
5    1.552405e+09
6    1.552405e+09
7    1.552405e+09
8    1.552405e+09
9    1.552405e+09
Name: t, dtype: float64
In [ ]:
jupyter_commit()
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmcmVzaCI6ZmFsc2UsImlkZW50aXR5Ijp7InVzZXJuYW1lIjoiSmllLVl1YW4iLCJpZCI6Njd9LCJ0eXBlIjoiYWNjZXNzIiwiZXhwIjoxNTUyOTY1NTkzLCJpYXQiOjE1NTIzNjA3OTMsIm5iZiI6MTU1MjM2MDc5MywianRpIjoiNjM1ZTg2MjQtYjA1ZC00NGJmLTljYjAtOGVjOGRmM2ExNmJkIn0.5jglhEGGs12ITl-DWWaFL-BVPhCzaDEeMKIJvEI-bbA [jovian] Saving notebook..
In [ ]: