Arrow date in a pandas column

Viewed 17

I am using arrow to get the dates of a single dataframe that has the following structure:

data=['2015', '2016','2017', '2108']
df= pd.DataFrame(data,columns=['time'])

I know that to get the date in arrow is with the following code:

arrow.get('2016')

Have tried to use this:

arrow.get(df['time'])

But it gives me this error: Cannot parse single argument of type <class 'pandas.core.series.Series'>.

How to tell arrow to use the column?

Thanks

1 Answers

Convert the entire series for access later

One option is to use pandas apply on the column. https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.apply.html

df.time = df.time.apply(lambda tm: arrow.get(tm))

Might be a way to do this with converters on load as well, depending on where you are loading from. csv docs for example, https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html

I also wonder why you are using arrow time versus pandas built in datetime type. Once again, depending on how you are loading this data, dtypes could be used to specify datetime.

Convert one value from the series

You need to choose one value instead of providing all values (i.e. the pd.Series)

arrow.get(df.time[1]) would convert 2016 in your example.

Related