Reputation: 469
I have no idea why but my DataFrame is showing the dates (index) in different formats. Not sure how to fix it.
My Code:
csv_m = pd.read_csv('coins_mktcap.csv')
data_m = pd.DataFrame(csv_m)
data_m['date'] = pd.to_datetime(data_m['date'])
data_m.set_index('date', inplace = True)
df_m = data_m.dropna(axis =1, how='all')
csv_m output
date
12/01/17
13/01/17
14/01/17
...
df_m output
date
2017-12-01
2017-01-13
2013-01-14
..
2017-01-02
2017-02-02
I expect a clean df_m output
date
2017-01-12
2017-01-13
2013-01-14
Upvotes: 1
Views: 60
Reputation: 863751
Add parameters parse_dates
and index_col
for DatetimeIndex
in read_csv
, also because format with starting days add dayfirst=True
, check also docs:
df_m = pd.read_csv('coins_mktcap.csv', parse_dates=['date'], index_col=['date'], dayfirst=True)
df_m = df_m.dropna(axis=1, how='all')
In your solution is possible add same parameter:
data_m['date'] = pd.to_datetime(data_m['date'], dayfirst=True)
Or specify format:
data_m['date'] = pd.to_datetime(data_m['date'], format='%d/%m%y')
Upvotes: 4