Reputation: 2697
I have some wire thing when renaming the pandas Series by the datetime.date
import pandas as pd
a = pd.Series([1, 2, 3, 4], name='t')
I got a
is:
0 1
1 2
2 3
3 4
Name: t, dtype: int64
Then, I have:
ts = pd.Series([pd.Timestamp('2016-05-16'),
pd.Timestamp('2016-05-17'),
pd.Timestamp('2016-05-18'),
pd.Timestamp('2016-05-19')], name='time')
with ts
as:
0 2016-05-16
1 2016-05-17
2 2016-05-18
3 2016-05-19
Name: time, dtype: datetime64[ns]
Now, if I do:
ts_date = ts.apply(lambda x: x.date())
dates = ts_date.unique()
I got dates
as:
array([datetime.date(2016, 5, 16), datetime.date(2016, 5, 17),
datetime.date(2016, 5, 18), datetime.date(2016, 5, 19)], dtype=object)
I have two approaches. The wired thing is, if I do the following renaming (approach 1):
for one_date in dates:
a.rename(one_date)
print one_date, a.name
I got:
2016-05-16 t
2016-05-17 t
2016-05-18 t
2016-05-19 t
But if I do it like this (approach 2):
for one_date in dates:
a = pd.Series(a, name=one_date)
print one_date, a.name
2016-05-16 2016-05-16
2016-05-17 2016-05-17
2016-05-18 2016-05-18
2016-05-19 2016-05-19
My question is: why the method rename
does not work (in approach 1)?
Upvotes: 0
Views: 1332