[英]convert a Numpy Array to datetime.date pandas DataFrame
I have a numpy.ndarray
thats values are pandas._libs.tslib.Timestamp
我有一个
numpy.ndarray
是pandas._libs.tslib.Timestamp
Example: 例:
In: type(closedDate)
在:
type(closedDate)
Out: numpy.ndarray
出:
numpy.ndarray
In: type(closedDate[0])
在:
type(closedDate[0])
Out: pandas._libs.tslib.Timestamp
出:
pandas._libs.tslib.Timestamp
I would like to convert the contents of closedDate
into a list of datetime.date
我想将
closedDate
的内容转换为datetime.date
的列表
I have tried the following: 我尝试了以下方法:
for i in closedDate:
closedDate[i].to_datetime()
But get this error: 但是得到这个错误:
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
What to do? 该怎么办? :/
:/
I'd expect numpy to provide this functionality via astype
, so you would not even have to loop over the array explicitely: 我希望numpy通过
astype
提供此功能,因此您甚至不必显式地遍历数组:
dt_arr = closedDate.astype(np.datetime64)
check: 校验:
closedDate
Out:
array([Timestamp('2017-09-25 14:39:00'), Timestamp('2017-09-26 14:39:00'),
Timestamp('2017-09-27 14:39:00')], dtype=object)
type(closedDate)
Out: numpy.ndarray
type(closedDate[0])
Out: pandas._libs.tslib.Timestamp
type(dt_arr)
Out: numpy.ndarray
type(dt_arr[0])
Out: numpy.datetime64
[x.to_pydatetime().date() for x in closedDate]
Note, however, that you've probably made some kind of mistake to end up with a numpy array containing a pandas datatype. 但是请注意,您可能犯了某种错误,最终导致了一个包含pandas数据类型的numpy数组。 pandas Series and DataFrames are better equipped to handle pandas-specific types than numpy arrays are.
与numpy数组相比,pandas Series和DataFrames能够更好地处理特定于熊猫的类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.