#๐Ÿ”’ Convert Column of String Dates to Numpy Array of Floats Using Vectorization

16 messages ยท Page 1 of 1 (latest)

worthy sigil
#

given a Pandas.DataFrame with a column consisting of string dates all in the format: mm/dd/yyyy hh:mm:ss, how could convert this column into a numpy array of type int using vectorization

dapper ginkgoBOT
#

@worthy sigil

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

knotty hull
#

!docs pandas.to_datetime

dapper ginkgoBOT
#
pandas.to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False, utc=False, format=None, exact=_NoDefault.no_default, unit=None, ...)```
Convert argument to datetime.

This function converts a scalar, array-like, [`Series`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.html#pandas.Series) or [`DataFrame`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html#pandas.DataFrame)/dict-like to a pandas datetime object.
knotty hull
#

@worthy sigil start with this

#

Then you need to decide what you want 1 to be (a second, a minute, etc.)

worthy sigil
#

oh thanks

knotty hull
#

Ignore anything you see about using apply. That won't be vectorized

worthy sigil
#

Is this the right idea?

queued_time_col = pd.to_datetime(df['Original Time Queued'].str[11:20], format='%H:%M:%S %p', exact=True)
#

ValueError: time data "03:04:47 " doesn't match format "%H:%M:%S %p", at position 0.

#

Changed slice [11:20] to [11:22]:

queued_time_col = pd.to_datetime(df['Original Time Queued'].str[11:22], format='%H:%M:%S %p', exact=True)
#

Works now

#

!solved

dapper ginkgoBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.