[SciPy-User] pandas: strange result using df.date.tolist

elmar werling elmar@net4werling...
Thu Jun 20 14:23:08 CDT 2013


Hello,

I get rather strage results using pandas "tolist" with date time values. 
As an examples 2013-01-15 13:56:44 is converted to 1970-01-16 141:56:44.

The following script:
--------------------------------------------------------------
import pandas as pd
from datetime import datetime

file_name = 'test_file.xlsx'
reader = pd.ExcelFile(file_name)
sheets = reader.sheet_names
df = reader.parse(sheets[0], header=0,parse_cols='A,B')

date = []
for i in range(len(df)):
     yr = df.date[i].year
     mo = df.date[i].month
     dy = df.date[i].day
     hr = df.time[i].hour
     mi = df.time[i].minute
     sc = df.time[i].second
     _date = datetime(yr, mo, dy, hr, mi, sc)
     date.append(_date)

df['date2'] = date

print 'date from list'
print date
print
print 'date from pd.DataFrame'
print df['date2']
print
print 'date from df.date.values'
print df.date2.values
print
print 'pandas version: ', pd.__version__

--------------------------------------------------------------
gives

--------------------------------------------------------------
Python 2.7.3 (default, Jan  2 2013, 13:56:14)
[GCC 4.7.2] on linux2
Type "copyright", "credits" or "license()" for more information.

 >>>

date from list
[datetime.datetime(2013, 1, 15, 13, 56, 44), datetime.datetime(2013, 1, 
18, 8, 17, 13), datetime.datetime(2013, 1, 18, 9, 17, 13), 
datetime.datetime(2013, 1, 23, 11, 12, 2), datetime.datetime(2013, 1, 
28, 9, 21, 12)]

date from pd.DataFrame
0   2013-01-15 13:56:44
1   2013-01-18 08:17:13
2   2013-01-18 09:17:13
3   2013-01-23 11:12:02
4   2013-01-28 09:21:12
Name: date2, dtype: datetime64[ns]

date from df.date.values
[1970-01-16 141:56:44 1970-01-16 208:17:13 1970-01-16 209:17:13
  1970-01-16 75:12:02 1970-01-16 193:21:12]

pandas version:  0.11.0

--------------------------------------------------------------

Any help is wellcome
Elmar
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test_file.xlsx
Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Size: 9513 bytes
Desc: not available
Url : http://mail.scipy.org/pipermail/scipy-user/attachments/20130620/3cc894f6/attachment.bin 


More information about the SciPy-User mailing list