Python Forum
Need help passing date to pandas query
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help passing date to pandas query
#1
How do I pass the output of this prompt to a pandas search by date in excel?

import pandas as pd
TestedDateBegin = pd.to_datetime(input('Input date in mm-dd-yyyy format: '))
For example, if I input 2019-09-08 into above input prompt and run TestedDateBegin I get this output:

Timestamp('2019-09-08 00:00:00')

This search with the date hard coded works fine.

data = df.loc[df['emr_first_access_date'] >= '2019-09-08', ['site_name','subs_num','emr_id', ```'emr_first_access_date']]
But how do I pass the date inputted from the prompt so the user can search by any date?

This doesn't work:

data = df.loc[df['emr_first_access_date'] >= 'TestedDateBegin', ['site_name','subs_num','emr_id',
and throws a exception:


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
pandas/_libs/tslibs/conversion.pyx in pandas._libs.tslibs.conversion.convert_str_to_tsobject()

pandas/_libs/tslibs/np_datetime.pyx in pandas._libs.tslibs.np_datetime._string_to_dts()

ValueError: Error parsing datetime string "TestedDateBegin" at position 0

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
pandas/_libs/tslibs/conversion.pyx in pandas._libs.tslibs.conversion.convert_str_to_tsobject()

pandas/_libs/tslibs/parsing.pyx in pandas._libs.tslibs.parsing.parse_datetime_string()

~\AppData\Local\Continuum\anaconda3\lib\site-packages\dateutil\parser\_parser.py in parse(timestr, parserinfo, **kwargs)
   1357     else:
-> 1358         return DEFAULTPARSER.parse(timestr, **kwargs)
   1359 

~\AppData\Local\Continuum\anaconda3\lib\site-packages\dateutil\parser\_parser.py in parse(self, timestr, default, ignoretz, tzinfos, **kwargs)
    648         if res is None:
--> 649             raise ValueError("Unknown string format:", timestr)
    650 

ValueError: ('Unknown string format:', 'TestedDateBegin')

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\arrays\datetimes.py in wrapper(self, other)
    144             try:
--> 145                 other = _to_M8(other, tz=self.tz)
    146             except ValueError:

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\arrays\datetimes.py in _to_M8(key, tz)
     77         # this also converts strings
---> 78         key = Timestamp(key)
     79         if key.tzinfo is not None and tz is not None:

pandas/_libs/tslibs/timestamps.pyx in pandas._libs.tslibs.timestamps.Timestamp.__new__()

pandas/_libs/tslibs/conversion.pyx in pandas._libs.tslibs.conversion.convert_to_tsobject()

pandas/_libs/tslibs/conversion.pyx in pandas._libs.tslibs.conversion.convert_str_to_tsobject()

ValueError: could not convert string to Timestamp

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-2-702fd23c14bb> in <module>
----> 1 data = df.loc[df['emr_first_access_date'] >= 'TestedDateBegin', ['site_name','subs_num','emr_id', 'emr_first_access_date']]

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\ops.py in wrapper(self, other, axis)
   1714 
   1715             res_values = dispatch_to_index_op(op, self, other,
-> 1716                                               pd.DatetimeIndex)
   1717 
   1718             return self._constructor(res_values, index=self.index,

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\ops.py in dispatch_to_index_op(op, left, right, index_class)
   1189         left_idx = left_idx._shallow_copy(freq=None)
   1190     try:
-> 1191         result = op(left_idx, right)
   1192     except NullFrequencyError:
   1193         # DatetimeIndex and TimedeltaIndex with freq == None raise ValueError

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexes\datetimelike.py in wrapper(self, other)
    115                 other = other._values
    116 
--> 117             result = op(self._data, maybe_unwrap_index(other))
    118             return result
    119 

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\arrays\datetimes.py in wrapper(self, other)
    146             except ValueError:
    147                 # string that cannot be parsed to Timestamp
--> 148                 return ops.invalid_comparison(self, other, op)
    149 
    150             result = op(self.asi8, other.view('i8'))

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\ops.py in invalid_comparison(left, right, op)
   1056     else:
   1057         raise TypeError("Invalid comparison between dtype={dtype} and {typ}"
-> 1058                         .format(dtype=left.dtype, typ=type(right).__name__))
   1059     return res_values
   1060 

TypeError: Invalid comparison between dtype=datetime64[ns] and str
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. Duh. So 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
  No Output In Pandas DataFrame Query eddywinch82 1 1,903 Aug-17-2020, 09:25 PM
Last Post: eddywinch82
  replace nan values by mean group by date.year, date.month wissam1974 5 8,324 Feb-19-2020, 06:25 PM
Last Post: AnkitGupta
  Obtaining Correct Date In Pandas DataFrame eddywinch82 14 5,850 Feb-17-2020, 11:45 AM
Last Post: eddywinch82
  Pandas Dataframe to Google Big Query Ecniv 2 3,004 Nov-21-2019, 02:26 PM
Last Post: Ecniv
  Trying to Pass date to pandas search from input prompt curranjohn46 1 2,072 Oct-10-2019, 10:01 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
  Working with date indexes (pandas) dervast 0 1,935 Apr-05-2019, 01:29 PM
Last Post: dervast

Forum Jump:

User Panel Messages

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