Horacio Nesman
Horacio Nesman

Reputation: 111

extract the day in a specific format from a date_range

I want to get a list with only the 'day' from this list with dates. I generated the dates with pd.date_range and then transformed it into a string.

date = '20170101'
fecha = pd.date_range(date , periods = 6, freq = 'D')
fecha = pd.Series(fecha.format())

print fecha[2][8:10]

for i in fecha:
    print fecha[i][8:10]

this throws KeyError: '2017-01-01'

From my understanding this for loop should iterate over the list and get the 2 digits for the day.
The reason why I am doing this is because I need to create dates with this format '2017-4-4' (so there is only one digit for numbers below 10)

Can somebody help?
Thanks

Upvotes: 2

Views: 675

Answers (3)

EdChum
EdChum

Reputation: 394051

You can create a datetime str using dt.strftime, then cast to str using astype(str), then split on '-' cast to int to get rid of the leading 0, you then cast back to str so we can then join the datetime components back together again:

In [105]:    
new_fecha = fecha.dt.strftime('%Y-%m-%d').str.split('-',expand=True).astype(int).astype(str)
new_fecha[0] + '-' + new_fecha[1] + '-' + new_fecha[2]

Out[105]:
0    2017-1-1
1    2017-1-2
2    2017-1-3
3    2017-1-4
4    2017-1-5
5    2017-1-6
dtype: object

It looks like your fecha is a DatetimeIndex this doesn't need .dt:

new_fecha = fecha.strftime('%Y-%m-%d').str.split('-',expand=True).astype‌​(int).astype(str)

So the above should work

Upvotes: 1

piRSquared
piRSquared

Reputation: 294288

The fix is simple, but I'm going to attempt to explain what you've done.

You made a variable named fecha that is a pandas.Series object with an index ranging from 0 to 5 and values which are strings that look like dates:

print(fecha)

0    2017-01-01
1    2017-01-02
2    2017-01-03
3    2017-01-04
4    2017-01-05
5    2017-01-06
dtype: object
#      ^
# Notice dtype object.  Usually indicative of strings.

Now when you iterate with the for loop, are you iterating through the index? Or the values? In other words, what is i?

Let's print and see

for i in fecha:
    print(i)

2017-01-01
2017-01-02
2017-01-03
2017-01-04
2017-01-05
2017-01-06

Ahhhh! Those are the string values and not the indices. Ok, now that we know this we can explain why fecha[i] didn't work. That's because the [] on a pandas.Series will attempt to grab the value from fecha whose index is i. But! i is the value and not the index.

Here are some various ways to rework this issue:

Option 1

for i in fecha:
    print(i[8:10])

01
02
03
04
05
06

Option 2
iteritems

for i, v in fecha.iteritems():
    print(fecha[i][8:10])

01
02
03
04
05
06

Option 3
.strftime also See This Link for strftime
Skip the series object and print from the datetime index

for d in pd.date_range(date, periods=6):
    print(d.strftime('%d'))

01
02
03
04
05
06

Upvotes: 1

John Moutafis
John Moutafis

Reputation: 23134

You are trying to access keys in your fecha which do not exist. This is happening at:

for i in fecha:
    print fecha[i][8:10]

(and probably here: print fecha[2][8:10] as well)
The cause is that i is not an index of fetcha but an item inside it.

Change it to:

for i in fecha:
    print i[8:10]

or

for i in range(len(fecha)):
    print fecha[i][8:10]

Upvotes: 1

Related Questions