Python Forum

Full Version: Trying to use 2 values from excel in my script but getting error..
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a script which reads in a text/csv files and im saving a filtered version as a new file, can be seen in my last post about deleting after a certain date.. That script works great by providing the date/time as a string value

I would like to be able to automatically pass that value in from the excel file that we are updating with the data..

I have the following code which works to display the values in the console window, but when i try to convert to string or datetime format for my filtering, i get errors

df = pd.read_excel(r'C:\Users\myexcel.xlsx)  

last_date = df.iloc[-1]['Date']  #pandas._libs.tslibs.timestamps.Timestamp

last_time = df.iloc[-1]['Time']  #datetime.time

If i try this:
date_val = (datetime.fromtimestamp(last_date) + ' ' + last_time)  i get error:  TypeError: an integer is required (got type Timestamp)

If i try this:
date_val = (last_date + ' ' + last_time)  i get error:  TypeError: unsupported operand type(s) for +: 'Timestamp' and 'str'
In my functioning script im trying to update these lines to use the above variables (the mask line is where im trying to plug the above into)

# THIS CONVERTS THE DATE COLUMN INTO A DATETIME FORMAT
df['DT'] = pd.to_datetime(df['Date'] + ' ' + df['Time'])

# HERE YOU NEED TO PROVIDE THE DATE YOU WANT TO KEEP GOING FORWARD
mask = (df['DT'] > '2022-05-09  2:31 PM')
what am i missing or doing wrong? I merely want to take the 2 values i get out of the excel file, to use as the filter that you can see on the "Mask" line above.
I think this is working, waiting to get a new test file to process, but i think this may work

# THIS WILL BUILD THE DATETIME STRING TO USE IN THE MASK FILTER
date_val = last_date.strftime('%Y-%m-%d') + ' ' + last_time.strftime("%I:%M %p")
That worked, sorry for posting the question, i posted and kept looking online and that seems to have done the trick
If there are no results, then there must be an error somewhere. Don't stress too much, relax and come back, we will definitely find a way to solve the problem Elastic man