Pandas 无法将输入转换为时间戳错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41343206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:40:50  来源:igfitidea点击:

Pandas cannot convert input to timestamp error

pythonpandas

提问by ajay

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.

我在数据集开始日期(格式 datetime64[ns])和结束日期(格式 datetime64[ns])中有两个变量。我正在使用以下代码来获取开始日期和结束日期之间的日期。

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.

而且我希望每一天都作为单独的记录,例如: begin_date = 01APR2015 和 end_date = 30APR2015,我希望每一天都作为单独的记录,如下所示。

 01APR2015
 02APR2015 etc

How can I get it as a separate record?

我怎样才能把它作为一个单独的记录?

Thanks in Advance.

提前致谢。

回答by MaxU

Assuming you have the following DF:

假设您有以下 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 seriesinstead of scalarvalues when calling pd.date_range()method:

我猜您在调用方法时尝试使用系列而不是量值pd.date_range()

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')

回答by Tian Wolf

Would you please help to format the input firstly, then the convert would works.

请您先帮助格式化输入,然后转换才能工作。

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')

回答by Alexander

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

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

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

如果有人从 2k20 遇到这个问题,解决方案是使用 lambda ...

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

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)

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