4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[python] np.arangeで任意の刻みの日付配列を作る

Posted at

モチベ

任意の刻みの日付配列を作りたい

コード

# 3ヶ月刻み
np.arange('2000-01', '2002-02',np.timedelta64(3,'M'), dtype='datetime64')
# ['2000-01' '2000-04' '2000-07' '2000-10' '2001-01' '2001-04' '2001-07' '2001-10' '2002-01']

# 2年刻み
np.arange('2010', '2018',np.timedelta64(2,'Y'), dtype='datetime64')
# ['2010' '2012' '2014' '2016']

# 1分刻み
np.arange('2000-01-01 10:00:00', '2000-01-01 10:05:00',np.timedelta64(1,'m'), dtype='datetime64')
# ['2000-01-01T10:00:00' '2000-01-01T10:01:00' '2000-01-01T10:02:00' '2000-01-01T10:03:00' '2000-01-01T10:04:00']

# おまけ # '2010'しか指定しなくても実際は細かい値を持ってます
np.datetime64('2010')==np.datetime64('2010-01-01 00:00:00')  # True
np.datetime64('2010')==np.datetime64('2010-01-01 00:00:01')  # False

numpyのdatetimeの方が慣れると使いやすそう

参考

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?