3

I have a DATE column like this,

    DATE   CÓDIGO  ...          UNIDADE  VALOR
0  2009.06  10000.0  ...              NaN    NaN
1  2009.06  10100.0  ...    NÃO SE APLICA      .
2  2009.06  10101.0  ...               M2   0.46
3  2009.06  10102.0  ...               UN  15.15

I want to convert it to date format %Y%m.

Applying the code,

df['DATA'] = pd.to_datetime(df['DATA'], format='%Y.%m')

I get this,

0   1970-01-01 00:00:00.000002009
1   1970-01-01 00:00:00.000002009
2   1970-01-01 00:00:00.000002009
3   1970-01-01 00:00:00.000002009
4   1970-01-01 00:00:00.000002009
Name: DATA, dtype: datetime64[ns]

Thanks for the help!

Janaka Ekanayake
  • 2,377
  • 10
  • 24
  • 48

2 Answers2

6

Convert to string first:

df['DATA'] = pd.to_datetime(df['DATA'].map('{:.2f}'.format), format='%Y.%m')

As it is, pd.to_datetime considers the float value is milliseconds since 1970.

IanS
  • 13,182
  • 7
  • 49
  • 75
2

Adding astype(str)

pd.to_datetime(df['DATE'].astype(str), format='%Y.%m')
Out[710]: 
0   2009-06-01
1   2009-06-01
2   2009-06-01
3   2009-06-01
Name: DATE, dtype: datetime64[ns]
BENY
  • 258,262
  • 17
  • 121
  • 165
  • 1
    I thought of that but I think controlling the format of the string conversion is safer :) – IanS Mar 19 '19 at 14:25