Reputation: 105
this is my datafarame with pandas
2020-02-05
2020-03-02
I wanna convert to int for plot to bar histogram chart. So i use regular expression to get only digit. after that i will try to convert int. but my regular expression function get error.
below function is to get digit in string
import re
def split_it(data):
return re.findall('(\d+)',data)[0]
and this is my error
expected string or bytes-like object
so i try again with another source code.
import re
def split_it(data):
return re.findall('(\d+)',str(data))
and result it's below.
[2020, 02, 05]
[2020, 03, 02]
[2020, 02, 19]
Upvotes: 2
Views: 144
Reputation: 114038
that doesnt look like a dataframe and you dont show us how you call it but here is how i would do it
series = pandas.to_datetime(['2020-02-05','2020-03-02'])
#this is actually a datetimeindex, that does not use .dt accessor
new_series = series.strftime("%Y%m%d").astype(int)
if it is a dataframe you would do it slightly different
#assumes a column named datetime exists, use its .dt accessor
int_series = df['datetime'].dt.strftime("%Y%m%d").astype(int)
Upvotes: 3