python中年月日_在Python中存储年月日

I simply need to store a month and a date (a birthday) WITHOUT a year value.

When looking at the documentation for pandas (mostly because it has useful time manipulation features), specifically Periods, it states that you need to pass in a timespan and a frequency. I'm not quite sure how to use the frequency part regarding to my problem. What frequency would be most appropriate? Is using pd even the best way to do this?

Thanks!

解决方案

That isn't how Period's work. They represent a specific period in time. Similar to how Timestamp's represent specific points in time.

You seem to want a generic Month-Day label. You can use strftime to get the string but you'll lose any date manipulation.

Consider the series of timestamps with timestamp indices.

s = pd.date_range('2011-01-31', periods=12, freq='M').to_series()

s

2011-01-31 2011-01-31

2011-02-28 2011-02-28

2011-03-31 2011-03-31

2011-04-30 2011-04-30

2011-05-31 2011-05-31

2011-06-30 2011-06-30

2011-07-31 2011-07-31

2011-08-31 2011-08-31

2011-09-30 2011-09-30

2011-10-31 2011-10-31

2011-11-30 2011-11-30

2011-12-31 2011-12-31

Freq: M, dtype: datetime64[ns]

You can get just the month and day like this (see this site for a summary of strftime)

s.dt.strftime('%m-%d')

2011-01-31 01-31

2011-02-28 02-28

2011-03-31 03-31

2011-04-30 04-30

2011-05-31 05-31

2011-06-30 06-30

2011-07-31 07-31

2011-08-31 08-31

2011-09-30 09-30

2011-10-31 10-31

2011-11-30 11-30

2011-12-31 12-31

Freq: M, dtype: object

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值