I have the code below which I want to keep the date itself only.
>>> date = q_data.Date
>>> dt = np.array(date)
>>> dt
array(['2017-02-14 09:00:00', '2017-02-15 09:00:00', '2017-02-16 09:00:00', '2017-02-17 09:00:00', '2017-02-20 09:00:00', '2017-02-21 09:00:00', '2017-02-22 09:00:00', '2017-02-23 09:00:00', '2017-02-24 09:00:00', '2017-02-28 09:00:00', '2017-03-01 09:00:00', '2017-03-02 09:00:00', '2017-03-03 09:00:00', '2017-03-06 09:00:00', '2017-03-07 09:00:00', '2017-03-08 09:00:00', '2017-03-09 09:00:00', '2017-03-10 09:00:00',] dtype=object)
I want it to have the following:
array(['2017-02-14','2017-02-15',....]dtyepe=object)
dt.date() return AttributeError: 'numpy.ndarray' object has no attribute 'date'
It looks like your input (date) is a series of string representations of date and time, with a space between the date and the time. If the format is guaranteed to be consistent with what you showed here, then you could use a list comprehension:
date = [x.split()[0] for x in date]
dt = np.array(date)
Alternatively, if date were a series of datetime objects, you could use:
date = [x.date().isoformat() for x in date]
dt = np.array(date)
Edit: If I'm understanding correctly that this is a Pandas dataframe with a single column, containing string representations of dates and times, the following would be one way to retain the dtype=object property:
def trim_to_date(s):
return s.split()[0]
date = date[0].apply(trim_to_date)
dt = np.array(date))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With