Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas cannot convert input to timestamp error

Tags:

python

pandas

I have two variables in the data set beginning date (format datetime64[ns]) and end date(format datetime64[ns]). I'm using following code to get the dates between beginning date and end date.

pd.date_range(start = data['beginning_date'], end = data['end_date'], freq = 'D')

but it's throwing following error.

cannot convert input to timestamp

why I'm getting above error. I tried changing as below, but it doesn't work.

pd.date_range(start = data['beginning_date'], end = data['end_date'], freq = 'D').astype('datetime')

and also i want each day as separate record, for example: beginning_date = 01APR2015 and end_date = 30APR2015, i want each day as separate record as below.

 01APR2015
 02APR2015 etc

How can I get it as a separate record?

Thanks in Advance.

like image 550
ajay Avatar asked Oct 30 '22 14:10

ajay


1 Answers

Assuming you have the following DF:

In [30]: df
Out[30]:
  beginning_date   end_date
0     2013-12-22 2014-01-01
1     2009-12-14 2009-12-28
2     2010-12-31 2011-01-11

I guess you tried to use series instead of scalar values when calling pd.date_range() method:

In [31]: pd.date_range(df.beginning_date, df.end_date)
...
skipped
...
TypeError: Cannot convert input to Timestamp

So try this instead:

In [32]: pd.date_range(df.beginning_date.min(), df.end_date.max())
Out[32]:
DatetimeIndex(['2009-12-14', '2009-12-15', '2009-12-16', '2009-12-17', '2009-12-18', '2009-12-19', '2009-12-20', '2009-12-21', '2009-12-22',
 '2009-12-23',
               ...
               '2013-12-23', '2013-12-24', '2013-12-25', '2013-12-26', '2013-12-27', '2013-12-28', '2013-12-29', '2013-12-30', '2013-12-31',
 '2014-01-01'],
              dtype='datetime64[ns]', length=1480, freq='D')
like image 98
MaxU - stop WAR against UA Avatar answered Nov 15 '22 07:11

MaxU - stop WAR against UA