将日期转换为天数,使用numpy的时间戳和datetime64。

huangapple go评论100阅读模式
英文:

Converting dates into days with numpy timestamp and datetime64

问题

You can resolve this issue by converting the "new_invoice" column to dtype 'datetime64[D]' using the .dt.date accessor. Here's the modified code:

  1. new_data['new_invoice'] = new_data['new_invoice'].dt.date

This will convert the "new_invoice" column to numpy datetime64 with a daily resolution, as you wanted.

英文:

I have the following code

  1. dates = data['InvoiceDate'].unique()
  2. c = data['StockCode'].unique()
  3. r = pd.DataFrame([[dates], [c]]).T
  4. r.columns=['InvoiceDate', 'StockCode']
  5. r = r.explode(column='InvoiceDate')
  6. r = r.explode(column='StockCode').reset_index(drop=True)
  7. new_data = pd.merge(r, data, how='left', on=['InvoiceDate', 'StockCode'])
  8. new_data['Quantity'] = new_data['Quantity'].fillna(0)
  9. new_data['new_invoice'] = np.array(new_data['InvoiceDate'], dtype='datetime64[D]')
  10. new_data.info()

I want to specify dtype of the column "new_invoice" as 'datetime64[D]'.
Unfortunately it seems that it doesn't work, because I got the following output

  1. <class 'pandas.core.frame.DataFrame'>
  2. Int64Index: 1241350 entries, 0 to 1241349
  3. Data columns (total 5 columns):
  4. # Column Non-Null Count Dtype
  5. --- ------ -------------- -----
  6. 0 InvoiceDate 1241350 non-null datetime64[ns]
  7. 1 StockCode 1241350 non-null object
  8. 2 Quantity 1241350 non-null float64
  9. 3 UnitPrice 280451 non-null float64
  10. 4 new_invoice 1241350 non-null datetime64[ns]
  11. dtypes: datetime64[ns](2), float64(2), object(1)
  12. memory usage: 56.8+ MB

and

  1. new_data['new_invoice'][0]

gives the following

  1. Timestamp('2010-12-01 00:00:00')

What I want to see is

  1. numpy.datetime64('2010-12-01')

Any ideas how to resolve it, please?

答案1

得分: 1

根据这个答案,你不能在pandas数据帧中存储datetime64[D]。但是,你可以使用以下方法获得一个datetime64[D]的numpy数组:

  1. new_invoice_dates = df["new_invoice"].values.astype('datetime64[D]')

它的类型将是datetime64[D]

英文:

As per this answer, you cannot store a datetime64[D] in a pandas dataframe. However, you can get out a datetime64[D] numpy array using the following:

  1. new_invoice_dates = df["new_invoice"].values.astype('datetime64[D]')

which will be of type datetime64[D].

huangapple
  • 本文由 发表于 2023年5月29日 22:29:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358181.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定