关于python&& pandas 的时间处理

image.png

python的时间处理和格式化 比较简单
pandas 在数据分析 时间序列时 也非常强悍
搞明白这些对于操作时间格式来说非常重要

参考

python
http://python3-cookbook.readthedocs.io/zh_CN/latest/c14/p07_catching_all_exceptions.html

[Pandas中时间和日期处理](

https://segmentfault.com/a/1190000011145901

Pandas中时间和日期处理
https://www.jianshu.com/p/96ea42c58abe

[Python中获取当前日期的格式]

https://www.cnblogs.com/wenBlog/p/6023742.html

python+pandas+时间、日期以及时间序列处理
https://blog.csdn.net/ly_ysys629/article/details/73822716

[Python日期的加减等操作]

https://www.cnblogs.com/vampirejt/p/4159267.html

[PYTHON PANDAS之数据重塑(Data Reshaping)]

https://segmentfault.com/a/1190000013334466

[python_pandas学习]

https://segmentfault.com/a/1190000014378986

Pandas dtypes(数据类型)

https://blog.csdn.net/claroja/article/details/72622375?utm_source=itdadao&utm_medium=referral

现实中常用的操作

ras=pd.read_csv(path,sep=',',encoding='gbk',header=0,parse_dates=["shouxin_day"],dtype={'phone':np.str})

ras.columns
Index(['shouxin_day', 'cus_flag', 'group_flag', 'flag', 'name', 'id_card',
       'phone', 'bank_card'],
      dtype='object')

ras["su"]=""
ras["sense"]="线上消费分期"

 dats=ras[[ 'name', 'id_card','phone','bank_card','shouxin_day','flag','su','su','su','su','sense','su']]

dp='/Users/geo/Downloads/AA17p4_new.txt'
dats.to_csv(dp,encoding='utf-8',sep='\t',header=False,index=False)


正常的  格式 转化      数字转字符   ,日期字符转 日期格式,这两类 都可以在 读取文件时就可以做  格式转化


pd.to_datetime(df)  用于 把 字符转 时间格式,可以是单列 ,也可以是 两列 三列 的组合

可以指定 format 的格式 并对处理erro 的防备
d=pd.to_datetime(raw['shouxin_day'],format='%Y-%m-%d',errors='ignore')

pd.to_datetime('13000101', format='%Y%m%d', errors='coerce')

甚至可以各个其实时间 ,做加法
pd.to_datetime([1, 2, 3], unit='D',origin=pd.Timestamp('1960-01-01'))


pd.Timestamp('2012-05-01')
pd.Timestamp(datetime(2012, 5, 1))
pd.Period('2011-01')
pd.Period('2012-05', freq='D')
pd.to_datetime(pd.Series(['Jul 31, 2009', '2010-01-10', None]))
pd.to_datetime(['2005/11/23', '2010.12.31'])


dates = [datetime(2012, 5, 1), datetime(2012, 5, 2), datetime(2012, 5, 3)]
 index = pd.DatetimeIndex(dates)

date_range日历,bdate_range工作日
In [40]: index = pd.date_range('2000-1-1', periods=1000, freq='M')

你可能感兴趣的:(关于python&& pandas 的时间处理)