0

consider this

df=pd.DataFrame({'A':['20150202','20150503','20150503'],'B':[3, 3, 1],'C':[1, 3, 1]})
df.A=pd.to_datetime(df.A)
df['month']=df.A.dt.to_period('M')

df
Out[59]: 
           A  B  C   month
0 2015-02-02  3  1 2015-02
1 2015-05-03  3  3 2015-05
2 2015-05-03  1  1 2015-05

and my month variable is:

df.month
Out[82]: 
0   2015-02
1   2015-05
2   2015-05
Name: month, dtype: object

Now if I index my dataset by df.month, it seems that Pandas understands this is a date. In other words, I can draw a plot without having to sort my index first.

But is this actually correct? The dtype object (instead of some datetime format) worries me. Is there a proper date object type for this kind of monthly date?

ℕʘʘḆḽḘ
  • 18,566
  • 34
  • 128
  • 235

1 Answers1

1

It is a pandas period object

In [5]: df.month.map(type)
Out[5]:
0    <class 'pandas._period.Period'>
1    <class 'pandas._period.Period'>
2    <class 'pandas._period.Period'>
Name: month, dtype: object
dmb
  • 1,669
  • 1
  • 12
  • 21
  • oh thats interesting! i did know about this map function. Is there any documentation? – ℕʘʘḆḽḘ Mar 25 '16 at 13:19
  • 1
    yep right here: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.map.html – dmb Mar 25 '16 at 13:20
  • 1
    and there are similar functions, some for Series, some for DataFrames. a good overview is here: https://stackoverflow.com/questions/19798153/difference-between-map-applymap-and-apply-methods-in-pandas – dmb Mar 25 '16 at 13:21