'expected string or buffer' when using re.match with pandas
<p>I am trying to clean some data from a csv file. I need to make sure that whatever is in the 'Duration' category matches a certain format. This is how I went about that:</p>

<pre><code>import re
import pandas as pd

data_path = './ufos.csv'
ufos = pd.read_csv(data_path)

valid_duration = re.compile('^[0-9]+ (seconds|minutes|hours|days)$')
ufos_clean = ufos[valid_duration.match(ufos.Duration)]

ufos_clean.head()
</code></pre>

<p>This gives me the following error:</p>

<pre><code>TypeErrorTraceback (most recent call last)
&lt;ipython-input-4-5ebeaec39a83&gt; in &lt;module&gt;()
      6 
      7 valid_duration = re.compile('^[0-9]+ (seconds|minutes|hours|days)$')
----&gt; 8 ufos_clean = ufos[valid_duration.match(ufos.Duration)]
      9 
     10 ufos_clean.head()

TypeError: expected string or buffer
</code></pre>

<p>I used a similar method to clean data before without the regular expressions. What am I doing wrong?</p>

<p>Edit:</p>

<p>MaxU got me the closest, but what ended up working was:</p>

<pre><code>valid_duration_RE = '^[0-9]+ (seconds|minutes|hours|days)$'
ufos_clean = ufos
ufos_clean = ufos_clean[ufos.Duration.str.contains(valid_duration_RE)]
</code></pre>

<p>There's probably a lot of redundancy in there, I'm pretty new to python, but it worked.</p>

<p>I guess you want it the other way round (not tested):</p>

<pre><code>import re
import pandas as pd

data_path = './ufos.csv'
ufos = pd.read_csv(data_path)

def cleanit(val):
    # your regex solution here
    pass

ufos['ufos_clean'] = ufos['Duration'].apply(cleanit)
</code></pre>

<p>After all, <code>ufos</code> is a <code>DataFrame</code>.</p>

python regex pandas dataframe