SciRuby / daru

Data Analysis in RUby
BSD 2-Clause "Simplified" License
1.03k stars 139 forks source link

How to convert Index to DateTimeIndex #486

Closed kojix2 closed 5 years ago

kojix2 commented 5 years ago

Hello.

I have one question. What is the best way to convert Index to DateTimeIndex?

I try

df
#=> #<Daru::DataFrame(574x5)>
#                  date        pce        pop    psavert    uempmed   unemploy
#          1 1967-07-01      507.4     198712       12.5        4.5       2944
#          2 1967-08-01      510.5     198911       12.5        4.7       2945
#          3 1967-09-01      516.3     199113       11.7        4.6       2958
#        ...        ...        ...        ...        ...        ...        ...

df.set_index 'date'
#=> #<Daru::DataFrame(574x5)>
#                   pce        pop    psavert    uempmed   unemploy
# 1967-07-01      507.4     198712       12.5        4.5       2944
# 1967-08-01      510.5     198911       12.5        4.7       2945
# 1967-09-01      516.3     199113       11.7        4.6       2958
#        ...        ...        ...        ...        ...        ...

df.index
# => #<Daru::Index(574): {1967-07-01, 1967-08-01, 1967-09-01, 1967-10-01, 1967-11-01, 1967-12-01, 1968-01-01, 1968-02-01, 1968-03-01, 1968-04-01, 1968-05-01, 1968-06-01, 1968-07-01, 1968-08-01, 1968-09-01, 1968-10-01, 1968-11-01, 1968-12-01, 1969-01-01, 1969-02-01 ... 2015-04-01}>

df.index = Daru::DateTimeIndex.new(df.index)
#=> #<Daru::DateTimeIndex(574) 1967-07-01...2015-04-01>

df.index
#=> #<Daru::DateTimeIndex(574) 1967-07-01...2015-04-01>

Yes. It work fine. Good! But I think df.index = Daru::DateTimeIndex.new(df.index) a little bit long.
Is there a method like df.index.to_datetime ?

kojix2 commented 5 years ago

Close this problem because it is low-priority issue.