8

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.

2
  • Please provide a Minimal, Complete, and Verifiable example Commented Dec 27, 2016 at 10:33
  • presumably you read the docs so you'd know that only valid types of args are datetimes or strings? Commented Dec 27, 2016 at 12:30

3 Answers 3

8

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')
Sign up to request clarification or add additional context in comments.

Comments

0

If you format the input first in this way, then the conversion would work.

pd.date_range(start = '30-APR-2015', end = '05-MAY-2015', freq = 'D')

And the output would be

DatetimeIndex(['2015-04-30', '2015-05-01', '2015-05-02', '2015-05-03',
           '2015-05-04', '2015-05-05'],
          dtype='datetime64[ns]', freq='D')

3 Comments

Thanks, for pd.date_range to work, are there any specific input formats that we need to follow? my input data has format datetime64[ns], to which format i need to change this?
After quick debug, the failure exists here: 426 if start is not None: --> 427 start = Timestamp(start) 428 429 if end is not None:
I can't edit my comments now. So I added it here. After quick debug, the failure exists in pandas.tslib.Timestamp.__new__. 426 if start is not None: --> 427 start = Timestamp(start) And according to the error code, the format is based on the datetime format.You may check the code for details.
-5

Если кто придет сюда с этой проблемой из 2к20, то решение - применение lambda

If anyone comes here with this problem from 2k20, the solution is to use lambda...

f = lambda x: len(pd.date_range(start=x[0],end=x[1],freq='M'))

df_clear['difference'] = df_clear[['start_month','finish_month']].apply(f, axis=1)

1 Comment

Welcome to Stack Overflow! Please format your code properly, click here to learn how. Also, please write your answer in English, as Stack Overflow is an English site.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.