python - Type error: cannot substract DateTimeArray from ndarray - Stack Overflow

I am trying to find the difference between 2 time and dates and result to be in a new column from a csv

I am trying to find the difference between 2 time and dates and result to be in a new column from a csv file(df).. Appreciate any help here..

   elif file_path.endswith(".csv"):
        df = pd.read_csv(file_path)
        break
    else:
        df = pd.read_excel(file_path, engine="openpyxl")
        break

# Transformations
df["open_time"] = np.where(
    (
        pd.to_datetime(df["Date/Time Opened"]).dt.date
        + pd.DateOffset(hours=17)
        - pd.to_datetime(df["Date/Time Opened"])
    ).dt.total_seconds()

I am trying to find the difference between 2 time and dates and result to be in a new column from a csv file(df).. Appreciate any help here..

   elif file_path.endswith(".csv"):
        df = pd.read_csv(file_path)
        break
    else:
        df = pd.read_excel(file_path, engine="openpyxl")
        break

# Transformations
df["open_time"] = np.where(
    (
        pd.to_datetime(df["Date/Time Opened"]).dt.date
        + pd.DateOffset(hours=17)
        - pd.to_datetime(df["Date/Time Opened"])
    ).dt.total_seconds()
Share Improve this question edited Mar 20 at 20:33 marc_s 757k184 gold badges1.4k silver badges1.5k bronze badges asked Mar 3 at 18:41 Sunil MSunil M 1 1
  • always put full error message because there are other useful information. – furas Commented Mar 5 at 3:10
Add a comment  | 

1 Answer 1

Reset to default 1

This is a typing issue - you just need to convert them to date time and then you can do the date math...

df["Date/Time Opened"] = pd.to_datetime(df["Date/Time Opened"])
df["Date/Time Closed"] = pd.to_datetime(df["Date/Time Closed"])

df["Duration (hours)"] = (df["Date/Time Closed"] - df["Date/Time Opened"]).dt.total_seconds() / 3600

Dates are stored in seconds, so the divide by 3600 is simply converting the seconds of the datetime fields into days in this instance.

Make sense?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745077401a4609917.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信