Bottom Page

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!
Quote
#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']]
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to predict with date as input for DecisionTreeRegressor sandeep_ganga 0 53 Dec-12-2019, 03:29 AM
Last Post: sandeep_ganga
  Need help passing date to pandas query curranjohn46 1 266 Oct-10-2019, 09:59 AM
Last Post: curranjohn46
  Pandas converting date to epoch randor 2 403 Jul-16-2019, 02:41 AM
Last Post: scidam
  replace nan values by mean group by date.year, date.month wissam1974 4 697 Jul-06-2019, 02:00 AM
Last Post: wissam1974
  Pandas and Date: problem with operator.How to resolve frame 6 1,227 May-13-2019, 05:50 PM
Last Post: frame
  Working with date indexes (pandas) dervast 0 396 Apr-05-2019, 01:29 PM
Last Post: dervast
  How to use pandas.rolling mean for 3D input array? Prv_Yadv 1 638 Mar-26-2019, 11:49 AM
Last Post: scidam
  Error: Slicing a single date from Pandas cheerful 0 654 Jul-22-2018, 08:09 PM
Last Post: cheerful
  Finding date count from a list of date range in pandas trillerducas72 0 701 May-24-2018, 02:30 AM
Last Post: trillerducas72
  Pandas .to_excel() Dropping First Row of Data When Run from Anaconda Prompt kazoli 0 749 Mar-15-2018, 04:52 PM
Last Post: kazoli

Forum Jump:


Users browsing this thread: 1 Guest(s)