添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

getting could not convert string to Timestamp error when trying pass pandas series data to date_range fucntion

Ask Question

I am trying to pass pandas series data to date_range fun and i am getting below error ,How can i cast string type to timestamp in pandas

import pandas as pd
import numpy as np
from pandas.tseries.offsets import CustomBusinessHour
from pandas.tseries.holiday import USFederalHolidayCalendar
data = {
    'start': ['2019-08-23 00:01:46.697000',
              '2018-10-29 19:01:10.887000',
              '2018-10-22 17:42:24.467000'],
    'end': ['2019-08-26 17::00.830000',
            '2018-11-27 09:31:39.967000',
            '2018-11-28 18:33:35.243000' ]
df = pd.DataFrame(data)
bh = CustomBusinessHour(calendar=USFederalHolidayCalendar(), start='00:01', end='23:59')
def f(x):
    idx = pd.date_range(start=x.start, end=x.end, freq= bh)
    mask = ~((idx.dayofweek == 0) & (idx.hour <= 7))
    return len(idx[mask])
df['Hours_diff'] = df.apply(f, axis=1)
print(df.head(10))

File "pandas/_libs/tslibs/timestamps.pyx", line 748, in pandas._libs.tslibs.timestamps.Timestamp.new File "pandas/_libs/tslibs/conversion.pyx", line 288, in pandas._libs.tslibs.conversion.convert_to_tsobject File "pandas/_libs/tslibs/conversion.pyx", line 487, in pandas._libs.tslibs.conversion.convert_str_to_tsobject ValueError: ('could not convert string to Timestamp', u'occurred at index 0')

IIUC, you need to convert your date columns to datetime before applying function Try this:

pd.to_datetime(df['start'])
pd.to_datetime(df['end'])
bh = CustomBusinessHour(calendar=USFederalHolidayCalendar(), start='00:01', end='23:59')
def f(x):
    idx = pd.date_range(start=x.start, end=x.end, freq= bh)
    mask = ~((idx.dayofweek == 0) & (idx.hour <= 7))
    return len(idx[mask])
df['Hours_diff'] = df.apply(f, axis=1)
print(df.head(10))

Output:

                        start                         end  Hours_diff
0  2019-08-23 00:01:46.697000  2019-08-26 17:00:00.830000          34
1  2018-10-29 19:01:10.887000  2018-11-27 09:31:39.967000         426
2  2018-10-22 17:42:24.467000  2018-11-28 18:33:35.243000         574
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.