Python Forum
Trying to Pass date to pandas search from input prompt
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to Pass date to pandas search from input prompt
#1
Hi,

I am trying to figure out how to pass a date inputted at a prompt by the user to pandas to search by date. I have both the search and the input prompt working separately but not together. I will show you what I mean. And maybe someone can tell me how to properly pass the date to pandas for the search.

This is how I successfully use pandas to extract rows in an excel sheet if any cell in column emr_first_access_date is greater than or equal to '2019-09-08'

I do this successfully with the following code:

import pandas as pd
HISorigFile = "C:\\folder\\inputfile1.xlsx"
#opens excel worksheet
df = pd.read_excel(HISorigFile, sheet_name='Non Live', skiprows=8)
#locates the columns I want to write to file including  date column emr_first_access_date if greater than or equal to '2019-09-08'
data = df.loc[df['emr_first_access_date'] >= '2019-09-08', ['site_name','subs_num','emr_id', 'emr_first_access_date']]
#sorts the data
datasort = data.sort_values("emr_first_access_date",ascending=False)
#this creates the file (data already sorted) in panda with date and time. 
datasort.to_excel(r'C:\\folder\sitesTestedInLastWeek.xlsx', index=False, header=True)
However, the date above is hardcoded of course. So, I need the user running this script to input the date. I created a very basic working input prompt with the following:

import datetime
#prompts for input date
TestedDateBegin = input('Enter beginning date to search for sites tested in YYYY-MM-DD format')
year, month, day = map(int, TestedDateBegin.split('-'))
date1 = datetime.date(year, month, day)
Obviously I want to pass TestedDateBegin to pandas, changing the pertinent code line:

data = df.loc[df['emr_first_access_date'] >= '2019-09-08', ['site_name','subs_num','emr_id', 'emr_first_access_date']]
to something like:

data = df.loc[df[b]['emr_first_access_date'] >= 'TestedDateBegin', ['site_name','subs_num','emr_id', 'emr_first_access_date']]
Obviously this doesn't work. But how do I proceed? I am very new to programming so I not always clear how to proceed. Does the date inputted in TestedDateBegin need to be added to a return? Or should it be put in a single item list? What is the right approach? Thx!
Reply
#2
This is resolved.

I had to remove the single quotes around TestedDateBegin as python, of course, interpreted that as a string and not a variable. Simple!

data = df.loc[df[b]['emr_first_access_date'] >= TestedDateBegin,['site_name','subs_num','emr_id', 'emr_first_access_date']]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pandas read csv file in 'date/time' chunks MorganSamage 4 1,647 Feb-13-2023, 11:24 AM
Last Post: MorganSamage
  Does a pandas have a date without a time? AlekseyPython 6 4,865 Feb-10-2021, 09:24 AM
Last Post: Naheed
  How to search for specific string in Pandas dataframe Coding_Jam 1 2,415 Nov-02-2020, 09:35 AM
Last Post: PsyPy
  replace nan values by mean group by date.year, date.month wissam1974 5 8,322 Feb-19-2020, 06:25 PM
Last Post: AnkitGupta
  Obtaining Correct Date In Pandas DataFrame eddywinch82 14 5,849 Feb-17-2020, 11:45 AM
Last Post: eddywinch82
  Matrix Search and Calculation Pandas luxlambo227 5 2,554 Jan-27-2020, 12:14 PM
Last Post: jefsummers
  How to predict with date as input for DecisionTreeRegressor sandeep_ganga 0 1,784 Dec-12-2019, 03:29 AM
Last Post: sandeep_ganga
  Need help passing date to pandas query curranjohn46 1 5,418 Oct-10-2019, 09:59 AM
Last Post: curranjohn46
  Pandas converting date to epoch randor 2 3,869 Jul-16-2019, 02:41 AM
Last Post: scidam
  Pandas and Date: problem with operator.How to resolve frame 6 4,270 May-13-2019, 05:50 PM
Last Post: frame

Forum Jump:

User Panel Messages

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