Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle NaT values for datetime when doing iterations in python?

Tags:

python

I am using below code to convert datetime values to integers. It works great except for NaT values. If I am doing this in an iteration, how can I handle NaT values so that I don't get errors such as 'NaTType does not support timetuple'?

import time
from datetime import datetime
t=datetime.now()
t1=t.timetuple()
int(time.mktime(t1)/60/60/24)

Here is the code to create sample data and what I have tried to iterate so far:

create data:

df = pd.DataFrame(data={'date':['05/16/16',''], 'Indicator':[1,0]})
df['date']=pd.to_datetime(df['date'])

Data:

   Indicator       date
0          1 2016-05-16
1          0        NaT

Iteration code:

def date2int(df):
    if df.date:
        t=df['date']
        t1=t.timetuple()
        return int(time.mktime(t1))
df['date2int']=df.apply(date2int,axis=1)

Error message:

Traceback (most recent call last):

File "", line 1, in df['date2int']=df.apply(date2int,axis=1)

File "/Users/Chen/anaconda/lib/python2.7/site-packages/pandas/core/frame.py", line 4042, in apply return self._apply_standard(f, axis, reduce=reduce)

File "/Users/Chen/anaconda/lib/python2.7/site-packages/pandas/core/frame.py", line 4138, in _apply_standard results[i] = func(v)

File "", line 4, in date2int t1=t.timetuple()

File "pandas/tslib.pyx", line 723, in pandas.tslib._make_error_func.f (pandas/tslib.c:16109)

ValueError: ('NaTType does not support timetuple', u'occurred at index 1')

like image 401
CWeeks Avatar asked May 17 '16 04:05

CWeeks


People also ask

How does Python handle NaT?

To test element-wise for NaT, use the numpy. isnat() method in Python Numpy. It checks the value for datetime or timedelta data type. The condition is broadcast over the input.

What is NaT in Python pandas?

nat means a missing date. Copy. df['time'] = pd. Timestamp('20211225') df. loc['d'] = np.


1 Answers

Solution #1:

def date2int(df):
    if df.date:
        t=df['date']
        try:
            t1=t.timetuple()
            return int(time.mktime(t1))
        except ValueError:
            return None
df['date2int']=df.apply(date2int,axis=1)

Solution #2:

df=df.dropna()
like image 111
Sergey Gornostaev Avatar answered Oct 04 '22 18:10

Sergey Gornostaev