简体   繁体   English

熊猫date_range:下降纳秒

[英]Pandas date_range: drop nanoseconds

I'm trying to create the following date range series with quarterly frequency: 我正在尝试创建每季度一次的以下日期范围系列:

import pandas as pd
pd.date_range(start = "1980", periods = 5, freq = 'Q-Dec')

Which returns 哪个返回

DatetimeIndex(['1980-03-31', '1980-06-30', '1980-09-30', '1980-12-31',
              '1981-03-31'],
              dtype='datetime64[ns]', freq='Q-DEC')

However, when I output this object to CSV I see the time portion of the series. 但是,当我将此对象输出到CSV时,我看到了该系列的时间部分。 eg 例如

1980-03-31 00:00:00, 1980-06-30 00:00:00

Any idea how I can get rid of the time portion so when I export to a csv it just shows: 任何想法我如何摆脱时间部分,所以当我导出到csv时,它只显示:

1980-03-31, 1980-06-30

You are not seeing nano-seconds, you are just seeing the time portion of the datetime that pandas has created for the DatetimeIndex . 您没有看到纳秒级的信息,只是看到了pandasDatetimeIndex创建的datetimetime部分。 You can extract only the date portion with .date() . 您只能使用.date()提取date部分。

Code: 码:

import pandas as pd
dr = pd.date_range(start="1980", periods=5, freq='Q-Dec')

print(dr[0].date())

Results: 结果:

1980-03-31

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM