1

I want to fill a column with particular dates. First, I create a column of dates filled only with the date of today. Then, I compute the date I want to replace for each location. The index is in the following format: '%Y-%m-%d'

The computation works well. It is the last line which does not work

There is my code:

for j in range(1, 154):
    result['LTD_' + str(j)] = dt.date(dt.datetime.now().year,dt.datetime.now().month,dt.datetime.now().day) #creating a column of dates filled with today day
    for i in range(0, len(result.index)):
        date_selected = result.iloc[[i]].index
        date_month = add_months(date_selected,j)
        delivery_month = date_month.month
        delivery_year = date_month.year
        delivery = dt.date(delivery_year, delivery_month, result[(result.index.month == (delivery_month)) & (result.index.year == delivery_year)].index.day.min())
        delloc = result.index.get_loc(str(delivery))
        ltdloc = delloc - 3
        result.iloc[[i]]['LTD_' + str(j)] = dt.date(result.iloc[[ltdloc]].index.year, result.iloc[[ltdloc]].index.month, result.iloc[[ltdloc]].index.day)

The last line does not work to replace the today date by the computed date. Nothing happened. date.replace generates an error.

7
  • What is error? If use instaed last row result.iloc[[i]]['LTD_' + str(j)] = 0 it failed? If use print(dt.date(result.iloc[[ltdloc]].index.year, result.iloc[[ltdloc]].index.month, result.iloc[[ltdloc]].index.day)) it failed? Commented May 25, 2017 at 12:01
  • result.iloc[[i]]['LTD_' + str(j)] = 0, nothing happened. I have still the today date (2017-05-25). However, print(dt.date(result.iloc[[ltdloc]].index.year, result.iloc[[ltdloc]].index.month, result.iloc[[ltdloc]].index.day)) gives the good date in the format '%Y-%m-%d' I want to replace in result.iloc[[i]]['LTD_' + str(j)]. Commented May 25, 2017 at 12:36
  • Hmmm, without data it is really hard, but result['LTD_' + str(j)].iloc[i] or result['LTD_' + str(j)].iloc[[i]] should works. Commented May 25, 2017 at 12:39
  • It worked! Thank you very much. How did you know? Commented May 25, 2017 at 12:41
  • Or better result.loc[result.index[i], 'LTD_' + str(j)] Commented May 25, 2017 at 12:41

1 Answer 1

1

Now is ix deprecated, so need loc with selecting index by position:

result.loc[result.index[i], 'LTD_' + str(j)] = ...

Or iloc with get_loc for position of column name:

result.iloc[i, result.columns.get_loc('LTD_' + str(j))] = ...

Another solution is:

result['LTD_' + str(j)].iloc[[i]] = ...
Sign up to request clarification or add additional context in comments.

2 Comments

result['LTD_' + str(j)].iloc[[i]] solved my issue. Thank you very much jezrael.
I add it to answer ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.