0

I am using Juypter notebooks to look at some stock data. That being said, I have come across a weird error which I don't totally understand.

time_and_sales['Trade_time'] = time_and_sales['Trade_time'].astype(str)
start, stop, step = 0, -7, 1
time_and_sales['Trade_time'] = time_and_sales['Trade_time'].str.slice(start, stop, step)
time_and_sales['Trade_time'] = pd.to_datetime(time_and_sales['Trade_time'], format='%H:%M:%S')
print(type(time_and_sales['Trade_time']))
print(time_and_sales['Trade_time'].head(10))

Here is the code and here is the error

ValueError: time data '' does not match format '%H:%M:%S' (match)

Any help would be appreciated!

3
  • Please share sample input with the data where error is occurring. Commented Nov 23, 2020 at 5:38
  • There are some invaid date in the column, try setting errors to either coerce or ignore in pd.to_datetime
    – sushanth
    Commented Nov 23, 2020 at 5:40
  • what do you mean by "drop empty strings"? you could convert to datetime with errors='coerce', that will leave NaT for inconvertible strings. You can then drop those rows with dropna(). Commented Nov 23, 2020 at 6:51

1 Answer 1

0

to drop rows from time_and_sales, with "empty strings" in ['Trade_time'] try time_and_sales.drop(time_and_sales.Trade_time.str.contains('').index, inplace = True)