英文:
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:
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
dates = data['InvoiceDate'].unique()
c = data['StockCode'].unique()
r = pd.DataFrame([[dates], [c]]).T
r.columns=['InvoiceDate', 'StockCode']
r = r.explode(column='InvoiceDate')
r = r.explode(column='StockCode').reset_index(drop=True)
new_data = pd.merge(r, data, how='left', on=['InvoiceDate', 'StockCode'])
new_data['Quantity'] = new_data['Quantity'].fillna(0)
new_data['new_invoice'] = np.array(new_data['InvoiceDate'], dtype='datetime64[D]')
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
<class 'pandas.core.frame.DataFrame'>
Int64Index: 1241350 entries, 0 to 1241349
Data columns (total 5 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 InvoiceDate 1241350 non-null datetime64[ns]
1 StockCode 1241350 non-null object
2 Quantity 1241350 non-null float64
3 UnitPrice 280451 non-null float64
4 new_invoice 1241350 non-null datetime64[ns]
dtypes: datetime64[ns](2), float64(2), object(1)
memory usage: 56.8+ MB
and
new_data['new_invoice'][0]
gives the following
Timestamp('2010-12-01 00:00:00')
What I want to see is
numpy.datetime64('2010-12-01')
Any ideas how to resolve it, please?
答案1
得分: 1
根据这个答案,你不能在pandas数据帧中存储datetime64[D]
。但是,你可以使用以下方法获得一个datetime64[D]
的numpy数组:
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:
new_invoice_dates = df["new_invoice"].values.astype('datetime64[D]')
which will be of type datetime64[D]
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论