熊猫功能pd.to_datetime

时间:2015-06-05 19:35:19

标签: python pandas

我正在尝试将日期列表从月/日/年转换为月和日。所以从1989年6月2日到6/2。下面是我认为可行的代码行。

Time = pd.to_datetime(listofdates, format = "%d%m", exact = False)

我的输出类似于" 1900-06-02"。我希望自己得到月和日。当我绘制数据并且1900显示在x轴上时出现问题。我只想在主要x轴上使用Month,在x轴上使用Day。所以我想知道如何从x轴或输出数据本身中删除1900?我正在使用" plot_date()"在x轴上绘制我的日期数据。

1 个答案:

答案 0 :(得分:0)

For the purpose of demonstrating the solution here, let's assume that your dataset looks like this:

In [84]: df
Out[84]: 
         date
0  2014-01-05
1  2014-06-09
2  2015-10-23
3  2015-12-21
4  2013-09-30

In [85]: df.dtypes
Out[85]: 
date    object
dtype: object

In [86]: df['formatted_date'] = pd.to_datetime(df.date)

In [87]: df.formatted_date = df.formatted_date.dt.strftime('%m-%d')

In [88]: df
Out[88]: 
         date formatted_date
0  2014-01-05          01-05
1  2014-06-09          06-09
2  2015-10-23          10-23
3  2015-12-21          12-21
4  2013-09-30          09-30

I hope that helps!

相关问题