使用 Pandas,我可以通过mySeries.dt.date.
一个 numpy 列看起来如何?例子:
import pandas as pd
df = pd.DataFrame({"a": ["31.12.1999 23:59:12", "31.12.1999 23:59:13", "31.12.1999 23:59:14"], "b": [4, 5, 6]})
df["datetime"] = pd.to_datetime(df.a)
df["date"]=df.datetime.dt.date
print("df.columns:", df.columns)
df.columns: Index(['a', 'b', 'datetime', 'date'], dtype='object')
<!-->
# convert to numpy array
dfVal = df.values
# display datetime
print("dfVal[:,2]:", dfVal[:, 2])
dfVal[:,2]: [Timestamp('1999-12-31 23:59:12') Timestamp('1999-12-31 23:59:13')
Timestamp('1999-12-31 23:59:14')]
<!_->
# try to convert
dfVal[:, 2].dt.date
<!-->
Traceback (most recent call last):
File "/home/user/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2963, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-12-5cead683e881>", line 1, in <module>
dfVal[:, 2].dt.date
AttributeError: 'numpy.ndarray' object has no attribute 'dt'
跃然一笑
相关分类