Reputation: 91
I am trying to label the indices of a data-frame. Now, under normal circumstances, it should be like this:
pd.DataFrame(fruits, columns=['Name of Fruit', 'Price'],
index=['Aug. 01, 2017', 'Aug. 02, 2018'])
Now these indices are labelled for just two days of a month. For example, if i have to index them for whole month or for whole year using notations like A1 for August 1, A2 for August 2 ... so, what would be the ideal way of doing this?
I am trying to be less labor-intensive by not putting every date manually.
Upvotes: 1
Views: 56
Reputation: 210832
Try this:
index = pd.Series(pd.date_range(start='20170101', periods=99, freq='D')
.strftime('%b.%d')) \
.str.replace(r'(.).*?(\d+)', r'\1\2')
yields:
In [74]: index
Out[74]:
0 J01
1 J02
2 J03
3 J04
4 J05
5 J06
6 J07
7 J08
8 J09
9 J10
...
89 M31
90 A01
91 A02
92 A03
93 A04
94 A05
95 A06
96 A07
97 A08
98 A09
Length: 99, dtype: object
Explanation:
In [5]: pd.date_range(start='20170725', periods=10, freq='D').strftime('%b.%d')
Out[5]:
array(['Jul.25', 'Jul.26', 'Jul.27', 'Jul.28', 'Jul.29', 'Jul.30', 'Jul.31', 'Aug.01', 'Aug.02', 'Aug.03'],
dtype='<U6')
then we can replace those strings so that we will keep only first letters and numbers (day) at the end...
Upvotes: 1
Reputation:
You can use date_range
function.
# One year, daily
pd.date_range(start='20170801', end='20180731', freq='D')
# Starting from August 1, 2017 - 365 days
pd.date_range(start='20170801', periods=365, freq='D')
# Ending at July 31, 2018 - 365 days
pd.date_range(end='20180731', periods=365, freq='D')
These should replace the list you are passing as index. For example, pd.DataFrame(index=pd.date_range(start='20170801', end='20180731', freq='D'))
.
Upvotes: 2