pandas 更改 DateTimeIndex 的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17152719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change date of a DateTimeIndex
提问by scls
I have a csv file named data.csvsuch as
我有一个名为的 csv 文件,data.csv例如
TS;val
10:00;0.1
10:05;0.2
10:10;0.3
10:15;0.4
I read this csv file using this script
我使用这个脚本读取了这个 csv 文件
#!/usr/bin/env python
import pandas as pd
if __name__ == "__main__":
yyyy = 2013
mm = 2
dd = 1
df = pd.read_csv('data.csv', sep=';', parse_dates=[0], index_col=0)
print(df)
I get this
我明白了
val
TS
2013-06-17 10:00:00 0.1
2013-06-17 10:05:00 0.2
2013-06-17 10:10:00 0.3
2013-06-17 10:15:00 0.4
I would like to change date of every DateTimeIndex to 2013-02-01
我想将每个 DateTimeIndex 的日期更改为 2013-02-01
val
TS
2013-02-01 10:00:00 0.1
2013-02-01 10:05:00 0.2
2013-02-01 10:10:00 0.3
2013-02-01 10:15:00 0.4
What is the easier way to do this ?
什么是更简单的方法来做到这一点?
回答by Andy Hayden
Timestamps have a replacemethod (just like datetimes):
时间戳有一个replace方法(就像日期时间):
In [11]: df.index.map(lambda t: t.replace(year=2013, month=2, day=1))
Out[11]:
array([Timestamp('2013-02-01 10:00:00', tz=None),
Timestamp('2013-02-01 10:05:00', tz=None),
Timestamp('2013-02-01 10:10:00', tz=None),
Timestamp('2013-02-01 10:15:00', tz=None)], dtype=object)
So set your index to this:
因此,将您的索引设置为:
In [12]: df.index = df.index.map(lambda t: t.replace(year=2013, month=2, day=1))
Worth mentioning that you can pass in a date_parserfunction to read_csv, which might make more sense for you:
值得一提的是,您可以将date_parser函数传递给read_csv,这可能对您更有意义:
In [21]: df = pd.read_csv(file_name, sep=';', parse_dates=[0], index_col=0,
date_parser=lambda time: pd.Timestamp('2013/02/01 %s' % time))
In [22]: df
Out[22]:
val
TS
2013-02-01 10:00:00 0.1
2013-02-01 10:05:00 0.2
2013-02-01 10:10:00 0.3
2013-02-01 10:15:00 0.4
回答by Jeff
provide a date_parser
提供一个 date_parser
In [50]: pd.read_csv(StringIO(data), sep=';', parse_dates=[0],
index_col=0, date_parser=lambda x: Timestamp('20130201 %s' % x))
Out[50]:
val
TS
2013-02-01 10:00:00 0.1
2013-02-01 10:05:00 0.2
2013-02-01 10:10:00 0.3
2013-02-01 10:15:00 0.4
回答by Paco
I see two ways of doing this. The first one being the simplest: Use 'string'.split(' '). For the string bb jj, it will return a list of 2 elements bband jj, so just get the first element.
我看到有两种方法可以做到这一点。第一个是最简单的:使用'string'.split(' '). 对于 string bb jj,它将返回一个包含 2 个元素bb和的列表jj,因此只需获取第一个元素。
The second option, is to create a datetime object from the string, and reformat it the way you want. This solution is better in my opinion. If tomorrow you want another format, this will be much easier.
To do so: use the strptimefunction documented here: http://docs.python.org/2/library/datetime.html#datetime.datetime.strptime
第二个选项是从字符串创建一个日期时间对象,并按照您想要的方式重新格式化它。在我看来,这个解决方案更好。如果明天你想要另一种格式,这会容易得多。为此:使用strptime此处记录的函数:http: //docs.python.org/2/library/datetime.html#datetime.datetime.strptime
Then to return a string from a datetime object, simply use the strftime function. All the different format are available here: http://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior
然后要从 datetime 对象返回一个字符串,只需使用strftime 函数。所有不同的格式都可以在这里找到:http: //docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior

