Applying strptime function to pandas series

thron of three

I have a pandas DataSeries that contains a string formatted date in the form of:

2016-01-14 11:39:54

I would like to convert the string to a timestamp.

I am using the apply method to attemp to pass 'datetime.strptime' to each element of the series

date_series = date_string.apply(datetime.strptime, args=('%Y-%m-%d %H:%M:%S'))

When I run the code, I get the following error:

strptime() takes exactly 2 arguments (18 given)

my questions are (1) am I taking the correct approach, (2) why is strptime converting my args into 18 arguments?

root

Use pd.to_datetime:

date_series = pd.to_datetime(date_string)

In general it's best have your dates as Pandas' pd.Timestamp instead of Python's datetime.datetime if you plan to do your work in Pandas. You may also want to review the Time Series / Date functionality documentation.

As to why your apply isn't working, args isn't being read as a tuple, but rather as a string that's being broken up into 17 characters, each being interpreted as a separate argument. To make it be read as a tuple, add a comma: args=('%Y-%m-%d %H:%M:%S',).

This is standard behaviour in Python. Consider the following example:

x = ('a')
y = ('a',)
print('x info:', x, type(x))
print('y info:', y, type(y))

x info: a <class 'str'>
y info: ('a',) <class 'tuple'>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Applying a function to pandas dataframe

From Dev

Applying a function to a pandas col

From Dev

Applying a function to a pandas col

From Dev

Applying function to Pandas dataframe by column

From Dev

Applying function to Pandas dataframe by column

From Dev

applying a lambda function to pandas dataframe

From Dev

Change Series inplace in DataFrame after applying function on it

From Dev

Applying a function to multiple rows of a time series in R

From Dev

applying strptime to local data frame

From Java

Applying a custom aggregation function to a pandas DataFrame

From Dev

Applying aggregate function on columns of Pandas pivot table

From Dev

Applying function to a single column of a grouped data in pandas

From Dev

Pandas: Applying a function to each group independently

From Dev

Applying a lambda function to a column got failed in pandas

From Dev

python pandas applying for loop and groupby function

From Dev

Applying a function to a MultiIndex pandas.DataFrame column

From Dev

Applying iterative function to every group in pandas DataFrame

From Dev

Pandas - Applying Function to every other row

From Dev

Applying a lambda function to a column got failed in pandas

From Dev

Applying function to a single column of a grouped data in pandas

From Dev

Applying aggregate function on columns of Pandas pivot table

From Dev

Applying function to every other column in pandas dataframe

From Dev

How to subset a Pandas dataframe applying a function by date?

From Dev

Applying iterative function to every group in pandas DataFrame

From Dev

Applying function to groupby objects in python pandas

From Dev

Creating class object from pandas series and applying methods

From Dev

Renaming series column names after applying function and returning mutiple dataframes

From Dev

Function with conditional statement to pandas series

From Dev

Pandas series operator as function parameter

Related Related

  1. 1

    Applying a function to pandas dataframe

  2. 2

    Applying a function to a pandas col

  3. 3

    Applying a function to a pandas col

  4. 4

    Applying function to Pandas dataframe by column

  5. 5

    Applying function to Pandas dataframe by column

  6. 6

    applying a lambda function to pandas dataframe

  7. 7

    Change Series inplace in DataFrame after applying function on it

  8. 8

    Applying a function to multiple rows of a time series in R

  9. 9

    applying strptime to local data frame

  10. 10

    Applying a custom aggregation function to a pandas DataFrame

  11. 11

    Applying aggregate function on columns of Pandas pivot table

  12. 12

    Applying function to a single column of a grouped data in pandas

  13. 13

    Pandas: Applying a function to each group independently

  14. 14

    Applying a lambda function to a column got failed in pandas

  15. 15

    python pandas applying for loop and groupby function

  16. 16

    Applying a function to a MultiIndex pandas.DataFrame column

  17. 17

    Applying iterative function to every group in pandas DataFrame

  18. 18

    Pandas - Applying Function to every other row

  19. 19

    Applying a lambda function to a column got failed in pandas

  20. 20

    Applying function to a single column of a grouped data in pandas

  21. 21

    Applying aggregate function on columns of Pandas pivot table

  22. 22

    Applying function to every other column in pandas dataframe

  23. 23

    How to subset a Pandas dataframe applying a function by date?

  24. 24

    Applying iterative function to every group in pandas DataFrame

  25. 25

    Applying function to groupby objects in python pandas

  26. 26

    Creating class object from pandas series and applying methods

  27. 27

    Renaming series column names after applying function and returning mutiple dataframes

  28. 28

    Function with conditional statement to pandas series

  29. 29

    Pandas series operator as function parameter

HotTag

Archive