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
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\ in parse(timestr, parserinfo, **kwargs)
   1357     else:
-> 1358         return DEFAULTPARSER.parse(timestr, **kwargs)

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

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\ in wrapper(self, other)
    144             try:
--> 145                 other = _to_M8(other,
    146             except ValueError:

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\arrays\ 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\ in wrapper(self, other, axis)
   1715             res_values = dispatch_to_index_op(op, self, other,
-> 1716                                               pd.DatetimeIndex)
   1718             return self._constructor(res_values, index=self.index,

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\ 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\ in wrapper(self, other)
    115                 other = other._values
--> 117             result = op(self._data, maybe_unwrap_index(other))
    118             return result

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

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\ 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

TypeError: Invalid comparison between dtype=datetime64[ns] and str
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']]

Possibly Related Threads…
Thread Author Replies Views Last Post
  No Output In Pandas DataFrame Query eddywinch82 1 416 Aug-17-2020, 09:25 PM
Last Post: eddywinch82
  replace nan values by mean group by date.year, date.month wissam1974 5 3,014 Feb-19-2020, 06:25 PM
Last Post: AnkitGupta
  Obtaining Correct Date In Pandas DataFrame eddywinch82 14 2,089 Feb-17-2020, 11:45 AM
Last Post: eddywinch82
  Pandas Dataframe to Google Big Query Ecniv 2 1,439 Nov-21-2019, 02:26 PM
Last Post: Ecniv
  Trying to Pass date to pandas search from input prompt curranjohn46 1 607 Oct-10-2019, 10:01 AM
Last Post: curranjohn46
  Pandas converting date to epoch randor 2 1,451 Jul-16-2019, 02:41 AM
Last Post: scidam
  Pandas and Date: problem with operator.How to resolve frame 6 1,771 May-13-2019, 05:50 PM
Last Post: frame
  Working with date indexes (pandas) dervast 0 765 Apr-05-2019, 01:29 PM
Last Post: dervast
  Error: Slicing a single date from Pandas cheerful 0 1,041 Jul-22-2018, 08:09 PM
Last Post: cheerful
  Finding date count from a list of date range in pandas trillerducas72 0 1,176 May-24-2018, 02:30 AM
Last Post: trillerducas72

Forum Jump:

User Panel Messages

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