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>You need add parameter <code>parse_dates</code> to <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html" rel="nofollow"><code>read_csv</code></a> with column name converted to <code>datetime</code>:</p>

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

temp=u"""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"""
#after testing replace io.StringIO(temp) to filename
df = pd.read_csv(io.StringIO(temp), sep='\t', parse_dates=['date'])

print (df)
        date
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

print (df.dtypes)
date    datetime64[ns]
dtype: object
</code></pre>

<p>Another solution is add numbers for order of column <code>date</code> - in sample it is first column, so add <code>0</code> (python counts from <code>0</code>):</p>

<pre><code>df = pd.read_csv(io.StringIO(temp), sep='\t', parse_dates=[0])

print (df)
        date
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

print (df.dtypes)
date    datetime64[ns]
dtype: object
</code></pre>

<hr>

<p>Then need convert column 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> and cast to <code>int</code>:</p>

<pre><code>#unix time in ns
df.date = df.date.values.astype(np.int64)
print (df)
                  date
0  1388880000000000000
1  1388880000000000000
2  1388880000000000000
3  1388880000000000000
4  1388880000000000000
5  1388880000000000000
6  1388880000000000000
7  1388880000000000000
8  1388880000000000000

#unix time in us
df.date = df.date.values.astype(np.int64) // 1000
print (df)
               date
0  1388880000000000
1  1388880000000000
2  1388880000000000
3  1388880000000000
4  1388880000000000
5  1388880000000000
6  1388880000000000
7  1388880000000000
8  1388880000000000
</code></pre>



<pre><code>#unix time in ms
df.date = df.date.values.astype(np.int64) // 1000000
#df.date = pd.to_datetime(df.date, unit='ms')
print (df)
            date
0  1388880000000
1  1388880000000
2  1388880000000
3  1388880000000
4  1388880000000
5  1388880000000
6  1388880000000
7  1388880000000
8  1388880000000

#unix time in s
df.date = df.date.values.astype(np.int64) // 1000000000
print (df)
         date
0  1388880000
1  1388880000
2  1388880000
3  1388880000
4  1388880000
5  1388880000
6  1388880000
7  1388880000
8  1388880000
</code></pre>

python csv pandas lambda timestamp