pandas - Pandas DatetimeIndex 到数据框
问题描述
如何将 DatetimeIndex 更改为这样的简单数据框:
month
0 2013-07-31
1 2013-08-31
2 2013-09-30
3 2013-10-31
这是日期时间索引:
DatetimeIndex(['2013-07-31', '2013-08-31', '2013-09-30', '2013-10-31',
'2013-11-30', '2013-12-31', '2014-01-31', '2014-02-28',
'2014-03-31', '2014-04-30', '2014-05-31', '2014-06-30'],
dtype='datetime64[ns]', freq='M')
谢谢你。
解决方案
使用DataFrame
构造函数:
idx = pd.DatetimeIndex(['2013-07-31', '2013-08-31', '2013-09-30', '2013-10-31',
'2013-11-30', '2013-12-31', '2014-01-31', '2014-02-28',
'2014-03-31', '2014-04-30', '2014-05-31', '2014-06-30'],
dtype='datetime64[ns]', freq='M')
df = pd.DataFrame({'month':idx})
#alternative
#df = pd.DataFrame({'month':df1.index})
print (df)
month
0 2013-07-31
1 2013-08-31
2 2013-09-30
3 2013-10-31
4 2013-11-30
5 2013-12-31
6 2014-01-31
7 2014-02-28
8 2014-03-31
9 2014-04-30
10 2014-05-31
11 2014-06-30
推荐阅读
- android - 如何在Android中使用FFMPEG命令制作可以无限次播放/循环的视频?
- javascript - 我的 sapUI5 应用程序只跳转一次进入我的打印方法
- angular - ([ngmodel]) 对象在方法中更改时不会刷新数据
- python - 绘制对角网格线,相交线的角度可变
- sql - 生成随机数量的子记录
- azure - 使用 Azure Functions Python 批量循环记录记录
- javascript - 两个不同客户端js文件之间的socket.io通信
- javascript - 来自数据库坐标数组的 Googlemap 上的 GeoLocation 历史记录
- javascript - 如何在触摸设备中使用 javascript 在固定容器内水平滚动图像?
- sql - Oracle SQL:将每个 CSV 值作为一行返回