Python Forum
Trying to use 2 values from excel in my script but getting error..
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to use 2 values from excel in my script but getting error..
#1
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.
Reply
#2
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")
Reply
#3
That worked, sorry for posting the question, i posted and kept looking online and that seems to have done the trick
Reply
#4
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Search Excel File with a list of values huzzug 4 1,147 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Python script for excel sheet Nabil 4 3,224 Jun-01-2021, 05:09 AM
Last Post: Pedroski55
  Python script to summarize excel tables, then output a composite table? i'm a total n surfer349 1 2,290 Feb-05-2021, 04:37 PM
Last Post: nilamo
  Argparse error when inputting values tqader 2 2,822 Sep-11-2020, 07:42 PM
Last Post: buran
  Filtering Excel Document Data Based On Numerical Values eddywinch82 30 10,460 Feb-25-2020, 06:08 PM
Last Post: eddywinch82
  To Copy text values to Excel using Python ksasi2k3 16 15,556 Dec-26-2019, 11:59 AM
Last Post: ashutoshdeodhar
  Search Excel column values jonzee 1 2,568 Dec-21-2019, 02:38 AM
Last Post: Clunk_Head
  help with changing values in an excel sheet and saving to a new file Moneyman212 2 2,843 Nov-19-2019, 06:25 AM
Last Post: Moneyman212
  Error: too many values to unpack Mike 1 5,096 Oct-30-2019, 03:07 PM
Last Post: buran
  auto supply values to a python script(function) from text file metro17 4 2,483 Oct-26-2019, 01:25 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020