Home > OS >  How do I get a previous date from iterrows() function (Python and Pandas)
How do I get a previous date from iterrows() function (Python and Pandas)

Time:05-31

I have a dataframe that I'm going through as follows:

  for index, row in my_dataframe.iterrows():
        cursor.execute(""" UPDATE MY_TABLE SET COLUMN1 = %s, 
        COLUMN2 = %s WHERE TO_TIMESTAMP(DATE_COLUMN, 'YYYY-MM-DD') = %s AND COLUMN1 IS NULL""",
                                        (row['VALUE_A'],
                                         row['VALUE_B'],
                                         row['DATE_C']
                                         ))

What I need is to apply something like:

row['DATE_C'] -1 day. 

How can I do that? How can we do row manipulations in python / pandas data frame.

Now, I know if we took out the row['...'] syntax and this was basic sql, we could just use the - interval '1' day syntax to get this done. But I need to use this loop to achieve my goal. Thank you.

CodePudding user response:

You can use pandas.Timedelta

pd.to_datetime(row['DATE_C']) - pd.Timedelta(days=1)
  • Related