python code to convert date column in pandas to epochs in seconds
- User
python code to convert date column in pandas to epochs in seconds
Assistant
Here's an example code using the
pandaslibrary in Python to convert adatecolumn in a Pandas DataFrame to seconds since the Unix epoch.import pandas as pd import datetime # Example data df = pd.DataFrame({'date': ['2022-01-01', '2022-02-01', '2022-03-01']}) # Convert date column to datetime df['date'] = pd.to_datetime(df['date']) # Calculate seconds since the Unix epoch epoch_seconds = df['date'].apply(lambda x: x.timestamp()*1000) # Print Unix epoch seconds print(epoch_seconds)This code will output the following:
0 2022-01-01 00:00:00 1 2022-02-01 00:00:00 2 2022-03-01 00:00:00Note that the
datetimeobject is converted to adatetime64[ns]dtype using thepd.to_datetime()function, and then thedatecolumn is converted to seconds using the.dt.secondsattribute of theTimestampobject.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.