question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

pd.date_range Attribute_Error

See original GitHub issue

I’m using pandas 0.12 and numpy 1.7.1.

I’m trying to calculate the number of business days between two columns of dates. Not all rows have valid dates though, and pd.date_range does not have an ignore errors option, so I’m getting this error:

In [439]: td = pd.DataFrame({'a':[pd.Timestamp('2010-1-1'), pd.Timestamp('2010-2-1')], 'b':[pd.Timestamp('2010-1-10'), p
d.Timestamp('2010-2-9')]})

In [440]: td.apply(lambda row: len(pd.date_range(row['a'], row['b'], freq='B')), axis=1)
Out[440]:
0    6
1    7
dtype: int64

In [441]: td = pd.DataFrame({'a':[pd.Timestamp('2010-1-1'), pd.Timestamp('2010-2-1')], 'b':[pd.Timestamp('2010-1-10'), p
d.NaT]})

In [442]: td.apply(lambda row: len(pd.date_range(row['a'], row['b'], freq='B')), axis=1)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-442-30b1500c1594> in <module>()
----> 1 td.apply(lambda row: len(pd.date_range(row['a'], row['b'], freq='B')), axis=1)

C:\Python27\lib\site-packages\pandas\core\frame.pyc in apply(self, func, axis, broadcast, raw, args, **kwds)
   4414                     return self._apply_raw(f, axis)
   4415                 else:
-> 4416                     return self._apply_standard(f, axis)
   4417             else:
   4418                 return self._apply_broadcast(f, axis)

C:\Python27\lib\site-packages\pandas\core\frame.pyc in _apply_standard(self, func, axis, ignore_failures)
   4489                     # no k defined yet
   4490                     pass
-> 4491                 raise e
   4492
   4493

AttributeError: ("'NaTType' object has no attribute 'tz'", u'occurred at index 1')

In [443]:

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jrebackcommented, Oct 12, 2013

That is quite ineffficient as you’d have to construct the entire series. Try this instead. Period handingling of NaT is not supported currently, but easy enough to roll your own.

In [27]: def f(row):
   ....:     if pd.isnull(row['a']) or pd.isnull(row['b']):
   ....:         return np.nan
   ....:     return pd.Period(row['a'],freq='B')-pd.Period(row['b'],freq='B')
   ....: 

In [29]: td.apply(f,axis=1)
Out[29]: 
0    -5
1   NaN
dtype: float64
0reactions
jrebackcommented, Oct 12, 2013

see #5203

Read more comments on GitHub >

github_iconTop Results From Across the Web

pandas.date_range — pandas 1.5.2 documentation
Return a fixed frequency DatetimeIndex. Returns the range of equally spaced time points (where the difference between any two adjacent points is specified...
Read more >
pandas - 'module' object has no attribute 'date_range' in python
You can't find date_range because you're using pandas version 0.7.0, which is very old (~9 Feb 2012) in pandas time-- the current stable ......
Read more >
Python | pandas.date_range() method - GeeksforGeeks
date_range() is one of the general functions in Pandas which is used to return a fixed frequency DatetimeIndex. Syntax: pandas.date_range(start= ...
Read more >
How to Fix: module 'pandas' has no attribute 'dataframe'
One error you may encounter when using pandas is: AttributeError: module 'pandas' has no attribute 'dataframe'.
Read more >
Chapter 9: Advanced Data Wrangling With Pandas
AttributeError Traceback (most recent call last) <ipython-input-3-acaefb05bf10> ... Create an array of datetimes with pd.date_range() or pd.period_range().
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found