將浮點數轉換為 datatime64[ns] (Converting float into datatime64[ns])


問題描述

將浮點數轉換為 datatime64[ns] (Converting float into datatime64[ns])

此問題與此處的以下問題有關小時和時間轉換

我從上一個問題中得到了以下具有相應值的數組:

a = [[7.15, 7.45, 9.30, 10.45, 13.45, 15.15, 15.45, 21.30]]

它的值是浮點數,它們代表一天中的幾個小時,例如 7.15 等於7:15。現在我在 pandas 中使用以下公式來做一個 comaprison:

df.loc[([df['orders_time'] >= a[0]) & (df['orders_time'] <= a[1]), 'new_time'] = 10

它返回一個錯誤說:

Invalid comparison between dtype=datetime64[ns] and float64

我試圖更改 中值的格式a 我無法運行它。


參考解法

方法 1:

You can convert a into a list of times.

a = [pd.to_datetime(i, format = '%H.%M').time() for i in a[0]]

then you can compare time to time using:

df.loc[([df['orders_time'].dt.time >= a[0]) & (df['orders_time'].dt.time <= a[1]), 'new_time'] = 10 

(by Dave Willdubbbdan)

參考文件

  1. Converting float into datatime64[ns] (CC BY‑SA 2.5/3.0/4.0)

#datetime #Python






相關問題

NHibernate:HQL:從日期字段中刪除時間部分 (NHibernate:HQL: Remove time part from date field)

如何獲得在給定時間內發送超過 X 個數據包的 IP (How do I get IPs that sent more than X packets in less than a given time)

Памылка дадання даты пры адніманні ад 0:00 (Dateadd error when subtracting from 0:00)

查找與日曆相比缺失的日期 (Find missing date as compare to calendar)

CodeReview:java Dates diff(以天為單位) (CodeReview: java Dates diff (in day resolution))

顯示兩個給定時間之間的 15 分鐘步長 (display 15-minute steps between two given times)

如何在 C# 中獲取月份名稱? (How to get the month name in C#?)

fromtimestamp() 的反義詞是什麼? (What is the opposite of fromtimestamp()?)

構建 JavaScript 時缺少模塊 (Missing Module When Building JavaScript)

setTimeout 一天中的特定時間,然後停止直到下一個特定時間 (setTimeout for specific hours of day and then stop until next specific time)

將浮點數轉換為 datatime64[ns] (Converting float into datatime64[ns])

Python Dataframe 在連接時防止重複 (Python Dataframe prevent duplicates while concating)







留言討論