Convert date to timestamp using lambda function while reading the file
<p>I am reading csv file which contains date in this format:</p>

<pre><code>date
01/05/2014
01/05/2014
01/05/2014
01/05/2014
01/05/2014
01/05/2014
01/05/2014
01/05/2014
01/05/2014
</code></pre>

<p>I can not use date like this in string format, which I need to convert into numeric timestamp.</p>

<p>So I wrote this code:</p>

<pre><code>Train = pd.read_csv("train.tsv", sep='\t') 
Train['timestamp'] = pd.to_datetime(Train['date']).apply(lambda a: a.timestamp())
</code></pre>

<p>This give me:</p>

<blockquote>
  <p>Train['timestamp'] = pd.to_datetime(Train['date']).apply(lambda a: a.timestamp())<br>
  AttributeError: 'Timestamp' object has no attribute 'timestamp'</p>
</blockquote>

<p>Could you please correct me to get in timestamp in lambda?</p>

<p>Edit code :</p>

<pre><code>Train = pd.read_csv("data_scientist_assignment.tsv", sep='\t', parse_dates=['date'])
#print df.head()
# Train['timestamp'] = pd.to_datetime(Train['date']).apply(lambda a: a.timestamp())
Train['timestamp'] = Train.date.values.astype(np.int64)
x1=["timestamp", "hr_of_day"]
test=pd.read_csv("test.csv")
print(Train.columns)
print(test.columns)
model = LogisticRegression()
model.fit(Train[x1], Train["vals"])
print(model)
print model.score(Train[x1], Train["vals"])
</code></pre>

<p>Another short way to go about this is to use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.to_datetime.html" rel="nofollow">to_datetime()</a>:</p>

<pre><code>In [209]: df['date']
Out[209]: 
0    01/05/2014
1    01/05/2014
2    01/05/2014
3    01/05/2014
4    01/05/2014
5    01/05/2014
6    01/05/2014
7    01/05/2014
8    01/05/2014
Name: date, dtype: object

In [210]: df['date'] = pd.to_datetime(df['date'])

In [211]: df['date']
Out[211]: 
0   2014-01-05
1   2014-01-05
2   2014-01-05
3   2014-01-05
4   2014-01-05
5   2014-01-05
6   2014-01-05
7   2014-01-05
8   2014-01-05
Name: date, dtype: datetime64[ns]
</code></pre>

<p>Also, you can get seconds like this:</p>

<pre><code>In [232]: df['date'].astype(pd.np.int64) // 10**9
Out[232]: 
0    1388880000
1    1388880000
2    1388880000
3    1388880000
4    1388880000
5    1388880000
6    1388880000
7    1388880000
8    1388880000
Name: date, dtype: int64
</code></pre>

python csv pandas lambda timestamp