擅长:python、mysql、java
<p>时间戳有一个<code>replace</code>方法(就像datetimes一样):</p>
<pre><code>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)
</code></pre>
<p>因此,请将索引设置为:</p>
<pre><code>In [12]: df.index = df.index.map(lambda t: t.replace(year=2013, month=2, day=1))
</code></pre>
<p>值得一提的是,您可以将<code>date_parser</code>函数传递给<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.io.parsers.read_csv.html" rel="noreferrer">^{<cd3>}</a>,这可能对您更有意义:</p>
<pre><code>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
</code></pre>