python - pandas - check if date exists in dataframe
<p>I have a dataframe like this:</p>

<pre><code>      category  date            number
0      Cat1     2010-03-01      1
1      Cat2     2010-09-01      1
2      Cat3     2010-10-01      1
3      Cat4     2010-12-01      1
4      Cat5     2012-04-01      1
5      Cat2     2013-02-01      1
6      Cat3     2013-07-01      1
7      Cat4     2013-11-01      2
8      Cat5     2014-11-01      5
9      Cat2     2015-01-01      1
10     Cat3     2015-03-01      1
</code></pre>

<p>I would like to check if a date is exist in this dataframe but I am unable to. I tried various ways as below but still no use:</p>

<pre><code>if pandas.Timestamp("2010-03-01 00:00:00", tz=None) in df['date'].values:
    print 'date exist'

if datetime.strptime('2010-03-01', '%Y-%m-%d') in df['date'].values:
    print 'date exist'

if '2010-03-01' in df['date'].values:
    print 'date exist'  
</code></pre>

<p>The 'date exist' never got printed. How could I check if the date exist? Because I want to insert the none-existed date with number equals 0 to all the categories so that I could plot a continuously line chart (one category per line). Help is appreciated. Thanks in advance. </p>

<p>The last one gives me this: 
<code>FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison</code>
And the <code>date exist</code> not get printed. </p>

<p>I think you need convert to datetime first by <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.to_datetime.html" rel="nofollow"><code>to_datetime</code></a> and then if need select all rows use <a href="http://pandas.pydata.org/pandas-docs/stable/indexing.html#boolean-indexing" rel="nofollow"><code>boolean indexing</code></a>:</p>

<pre><code>df.date = pd.to_datetime(df.date)

print (df.date == pd.Timestamp("2010-03-01 00:00:00"))
0      True
1     False
2     False
3     False
4     False
5     False
6     False
7     False
8     False
9     False
10    False
Name: date, dtype: bool

print (df[df.date == pd.Timestamp("2010-03-01 00:00:00")])
  category       date  number
0     Cat1 2010-03-01       1
</code></pre>

<p>For return <code>True</code> use check value converted to <code>numpy array</code> by <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.values.html" rel="nofollow"><code>values</code></a>:</p>

<pre><code>if ('2010-03-01' in df['date'].values):
    print ('date exist')
</code></pre>

<p>Or at least one <code>True</code> by <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.any.html" rel="nofollow"><code>any</code></a> as comment <a href="http://stackoverflow.com/questions/39893420/python-pandas-check-if-date-exists-in-dataframe#comment67072665_39893420">Edchum</a>:</p>

<pre><code>if (df.date == pd.Timestamp("2010-03-01 00:00:00")).any():
    print ('date exist')  
</code></pre>

python datetime pandas dataframe