joyful pandas task10-时序数据

1.时间戳
1.Timestamp的构造与属性
ts = pd.Timestamp(‘2020/1/1’)
ts = pd.Timestamp(‘2020-1-1 08:10:30’)
属性:通过year, month, day, hour, min, second可以获取具体的数值。
2.Datetime序列的生成
1.date_range是一种生成连续间隔时间的一种方法,其重要的参数为start, end, freq, periods,它们分别表示开始时间,结束时间,时间间隔,时间戳个数。
pd.date_range(‘2020-1-1’,‘2020-1-21’, freq=‘10D’) # 包含
pd.date_range(‘2020-1-1’, ‘2020-2-28’, periods=6) # 由于结束日期无法取到,freq不为10天
2.最后,要介绍一种改变序列采样频率的方法asfreq,它能够根据给定的freq对序列进行类似于reindex的操作:
s = pd.Series(np.random.rand(5), index=pd.to_datetime([‘2020-1-%d’%i for i in range(1,10,2)]))
s.asfreq(‘D’).head()
s.asfreq(‘12H’).head()
3.dt对象
1.第一类操作的常用属性包括:date, time, year, month, day, hour, minute, second, microsecond, nanosecond, dayofweek, dayofyear, weekofyear, daysinmonth, quarter,其中daysinmonth, quarter分别表示月中的第几天和季度。
s = pd.Series(pd.date_range(‘2020-1-1’,‘2020-1-3’, freq=‘D’))
s.dt.date
可以通过month_name, day_name返回英文的月名和星期名,注意它们是方法而不是属性:
s.dt.month_name()
s.dt.day_name()
2.第二类判断操作主要用于测试是否为月/季/年的第一天或者最后一天:
s.dt.is_year_start # 还可选 is_quarter/month_start
s.dt.is_year_end # 还可选 is_quarter/month_end
3.第三类的取整操作包含round, ceil, floor,它们的公共参数为freq,常用的包括H, min, S(小时、分钟、秒)。
s = pd.Series(pd.date_range(‘2020-1-1 20:35:00’, ‘2020-1-1 22:35:00’, freq=‘45min’))
s.dt.round(‘1H’)
s.dt.ceil(‘1H’)
s.dt.floor(‘1H’)
4.时间戳的切片与索引
一般而言,时间戳序列作为索引使用。如果想要选出某个子时间戳序列,第一类方法是利用dt对象和布尔条件联合使用,另一种方式是利用切片,后者常用于连续时间戳。
Example1:每月的第一天或者最后一天
s[(idx.is_month_start|idx.is_month_end).values].head()
Example2:双休日
s[idx.dayofweek.isin([5,6]).values].head()
Example4:取出七月
s[‘2020-07’].head()
Example5:取出5月初至7月15日
s[‘2020-05’:‘2020-7-15’].head()

2.时间差
1.Timedelta的生成
1.与date_range一样,时间差序列也可以用timedelta_range来生成,它们两者具有一致的参数:
pd.timedelta_range(‘0s’, ‘1000s’, freq=‘6min’)
pd.timedelta_range(‘0s’, ‘1000s’, periods=3)
2.如果不想对天数取余而直接对应秒数,可以使用total_seconds
s.dt.total_seconds().head()
3.与时间戳序列类似,取整函数也是可以在dt对象上使用的:
pd.to_timedelta(df.Time_Record).dt.round(‘min’).head()
2.Timedelta的运算
时间差支持的常用运算有三类:与标量的乘法运算、与时间戳的加减法运算、与时间差的加减法与除法运算。这些运算都可以移植到时间差的序列上。

3.日期偏置
1.Offset对象
日期偏置是一种和日历相关的特殊时间差,例如回到第一节中的两个问题:如何求2020年9月第一个周一的日期,以及如何求2020年9月7日后的第30个工作日是哪一天。
pd.Timestamp(‘20200831’) + pd.offsets.WeekOfMonth(week=0,weekday=0)
pd.Timestamp(‘20200907’) + pd.offsets.BDay(30)
从上面的例子中可以看到,Offset对象在pd.offsets中被定义。当使用+时获取离其最近的下一个日期,当使用-时获取离其最近的上一个日期。
2.偏置字符串
前面提到了关于date_range的freq取值可用Offset对象,同时在pandas中几乎每一个Offset对象绑定了日期偏置字符串(frequencies strings/offset aliases),可以指定Offset对应的字符串来替代使用。
pd.date_range(‘20200101’,‘20200331’, freq=‘MS’) # 月初
等价:
pd.date_range(‘20200101’,‘20200331’,freq=pd.offsets.MonthBegin())

4.时序中的滑窗与分组
1. 滑动窗口
所谓时序的滑窗函数,即把滑动窗口用freq关键词代替。
对于shift函数而言,作用在datetime64为索引的序列上时,可以指定freq单位进行滑动:
s.shift(freq=‘50D’).head()
diff后就能够得到timedelta64[ns]的序列,这能够使用户方便地观察有序时间序列的间隔:
a=my_series.diff(1).head()#一阶差分
2.重采样
1.重采样对象resample和第四章中分组对象groupby的用法类似,前者是针对时间序列的分组计算而设计的分组对象。
s.resample(‘10D’).mean().head()
2.同时,如果没有内置定义的处理函数,可以通过apply方法自定义:
s.resample(‘10D’).apply(lambda x:x.max()-x.min()).head() # 极差
3.有时候,用户希望从序列的最小时间戳开始依次增加freq进行分组,此时可以指定origin参数为start:
s.resample(‘7min’, origin=‘start’).mean().head()
4.在返回值中,要注意索引一般是取组的第一个时间戳,但M, A, Q, BM, BA, BQ, W这七个是取对应区间的最后一个时间戳:
s = pd.Series(np.random.randint(2,size=366), index=pd.date_range(‘2020-01-01’, ‘2020-12-31’))
s.resample(‘M’).mean().head()
s.resample(‘MS’).mean().head() # 结果一样,但索引不同

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用代码解决这个问题The program committee of the school programming contests, which are often held at the Ural State University, is a big, joyful, and united team. In fact, they are so united that the time spent together at the university is not enough for them, so they often visit each other at their homes. In addition, they are quite athletic and like walking. Once the guardian of the traditions of the sports programming at the Ural State University decided that the members of the program committee spent too much time walking from home to home. They could have spent that time inventing and preparing new problems instead. To prove that, he wanted to calculate the average distance that the members of the program committee walked when they visited each other. The guardian took a map of Yekaterinburg, marked the houses of all the members of the program committee there, and wrote down their coordinates. However, there were so many coordinates that he wasn't able to solve that problem and asked for your help. The city of Yekaterinburg is a rectangle with the sides parallel to the coordinate axes. All the streets stretch from east to west or from north to south through the whole city, from one end to the other. The house of each member of the program committee is located strictly at the intersection of two orthogonal streets. It is known that all the members of the program committee walk only along the streets, because it is more pleasant to walk on sidewalks than on small courtyard paths. Of course, when walking from one house to another, they always choose the shortest way. All the members of the program committee visit each other equally often. Input The first line contains the number n of members of the program committee (2 ≤ n ≤ 105). The i-th of the following n lines contains space-separated coordinates xi, yi of the house of the i-th member of the program committee (1 ≤ xi, yi ≤ 106). All coordinates are integers. Output Output the average distance, rounded down to an integer, that a member of the program committee walks from his house to the house of his colleague.
05-26

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值