Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting column from int64 to datetime in hdf5 file using Python's Pandas package

I am new to Pandas and programming in general, so any help would be greatly appreciated.

I am having difficulty converting a column of data in a Pandas dataframe, loaded from an hdf5 file, to a datetime object. The data is too large to work with has a text file, so I converted it to an hdf5 file using the following code:

# get text file from zip file and unzip
file = urllib.request.urlretrieve(file, dir)           
z = zipfile.ZipFile(dir)             
data = z.open(z.namelist()[0])

# column names from text file
colnames = ['Patent#','App#','Small','Filing Date','Issue Date', 'Event Date', 'Event Code'] 

# load the data in chunks and concat into single DataFrame        
mfees = pd.read_table(data, index_col=0, sep='\s+', header = None, names = colnames, chunksize=1000, iterator=True)
df = pd.concat([chunk for chunk in mfees], ignore_index=False)

# close files        
z.close()
data.close()

# convert to hdf5 file
data = data.to_hdf('mfees.h5','raw_data',format='table')

After this my data is in the following format:

data['Filing Date']

Output:

Patent#
4287053    19801222
4287053    19801222
4289713    19810105
4289713    19810105
4289713    19810105
4289713    19810105
4289713    19810105
4289713    19810105
Name: Filing Date, Length: 11887679, dtype: int64

However, when I use the to_datetime function, I get the following:

data['Filing Date'] = pd.to_datetime(data['Filing Date'])
data['Filing Date']

Output:

Patent#
4287053   1970-01-01 00:00:00.019801222
4287053   1970-01-01 00:00:00.019801222
4289713   1970-01-01 00:00:00.019810105
4289713   1970-01-01 00:00:00.019810105
4289713   1970-01-01 00:00:00.019810105
4289713   1970-01-01 00:00:00.019810105
4289713   1970-01-01 00:00:00.019810105
4289713   1970-01-01 00:00:00.019810105
4289713   1970-01-01 00:00:00.019810105
4291808   1970-01-01 00:00:00.019801212
4291808   1970-01-01 00:00:00.019801212
4292069   1970-01-01 00:00:00.019810123
4292069   1970-01-01 00:00:00.019810123
4292069   1970-01-01 00:00:00.019810123
4292069   1970-01-01 00:00:00.019810123
Name: Filing Date, Length: 11887679, dtype: datetime64[ns]

I am not sure why I am getting the above output for the datetime object. Is there something I can do correct this and properly convert the dates to datetime objects? Thanks!

like image 688
Gerard Torres Avatar asked Oct 23 '25 03:10

Gerard Torres


1 Answers

Easiest just to convert when you are reading it in (note that I copy pasted your data, so you just need to add the parse_dates=[1] option

In [31]: df = read_csv(StringIO(data),sep='\s+',header=None,parse_dates=[1],names=['num','date']).set_index('num')

In [32]: df
Out[32]: 
                       date
num                        
4287053 1980-12-22 00:00:00
4287053 1980-12-22 00:00:00
4289713 1981-01-05 00:00:00
4289713 1981-01-05 00:00:00
4289713 1981-01-05 00:00:00
4289713 1981-01-05 00:00:00
4289713 1981-01-05 00:00:00
4289713 1981-01-05 00:00:00

In [33]: df.dtypes
Out[33]: 
date    datetime64[ns]
dtype: object

Then the hdf will handle the column

In [46]: df.to_hdf('test.h5','df',mode='w',format='table')

In [47]: pd.read_hdf('test.h5','df')
Out[47]: 
                       date
num                        
4287053 1980-12-22 00:00:00
4287053 1980-12-22 00:00:00
4289713 1981-01-05 00:00:00
4289713 1981-01-05 00:00:00
4289713 1981-01-05 00:00:00
4289713 1981-01-05 00:00:00
4289713 1981-01-05 00:00:00
4289713 1981-01-05 00:00:00

In [48]: pd.read_hdf('test.h5','df').dtypes
Out[48]: 
date    datetime64[ns]
dtype: object

Here's a converter for int-like dates, should be pretty fast

In [18]: s = Series([19801222,19801222] + [19810105]*5)

In [19]: s
Out[19]: 
0    19801222
1    19801222
2    19810105
3    19810105
4    19810105
5    19810105
6    19810105
dtype: int64

In [20]: s = s.values.astype(object)

In [21]: Series(pd.lib.try_parse_year_month_day(s/10000,s/100 % 100, s % 100))
Out[21]: 
0   1980-12-22 00:00:00
1   1980-12-22 00:00:00
2   1981-01-05 00:00:00
3   1981-01-05 00:00:00
4   1981-01-05 00:00:00
5   1981-01-05 00:00:00
6   1981-01-05 00:00:00
dtype: datetime64[ns]
like image 159
Jeff Avatar answered Oct 25 '25 17:10

Jeff