Convert Pandas Column to DateTime
Use the to_datetime
function, specifying a format to match your data.
raw_data['Mycol'] = pd.to_datetime(raw_data['Mycol'], format='%d%b%Y:%H:%M:%S.%f')
Convert DataFrame column type from string to datetime
The easiest way is to use to_datetime
:
df['col'] = pd.to_datetime(df['col'])
It also offers a dayfirst
argument for European times (but beware this isn't strict).
Here it is in action:
In [11]: pd.to_datetime(pd.Series(['05/23/2005']))
Out[11]:
0 2005-05-23 00:00:00
dtype: datetime64[ns]
You can pass a specific format:
In [12]: pd.to_datetime(pd.Series(['05/23/2005']), format="%m/%d/%Y")
Out[12]:
0 2005-05-23
dtype: datetime64[ns]
Convert Pandas column to datetime for a specific datetime format
You can use pd.to_datetime
for this.
import pandas as pd
df = pd.DataFrame({"date": ["2011-06-12T01:17:56"]})
Conversion using map method:
df["date"].map(pd.to_datetime)
or
Conversion using apply method:
df["date"].apply(pd.to_datetime)
or
Conversion using function on column series:
df["date"] = pd.to_datetime(df["date"])
Converting object column in pandas dataframe to datetime
It means you have an extra space. Though pd.to_datetime
is very good at parsing dates normally without any format specified, when you actually specify a format, it has to match EXACTLY.
You can likely solve your issue by adding .str.strip()
to remove the extra whitespace before converting.
import pandas as pd
df['Time stamp'] = pd.to_datetime(df['Time stamp'].str.strip(), format='%d/%m/%Y')
Alternatively, you can take advantage of its ability to parse various formats of dates by using the dayfirst=True
argument
df['Time stamp'] = pd.to_datetime(df['Time stamp'], dayfirst=True)
Example:
import pandas as pd
df = pd.DataFrame({'Time stamp': ['01/02/1988', '01/02/1988 ']})
pd.to_datetime(df['Time stamp'], format= '%d/%m/%Y')
ValueError: unconverted data remains:
pd.to_datetime(df['Time stamp'].str.strip(), format='%d/%m/%Y')
#0 1988-02-01
#1 1988-02-01
#Name: Time stamp, dtype: datetime64[ns]
pd.to_datetime(df['Time stamp'], dayfirst=True)
#0 1988-02-01
#1 1988-02-01
#Name: Time stamp, dtype: datetime64[ns]
How to convert a pandas datetime column from UTC to EST
tz_localize
and tz_convert
work on the index of the DataFrame. So you can do the following:
- convert the "time" to Timestamp format
- set the "time" column as index and use the conversion functions
reset_index()
- keep only the time
Try:
dataframe["time"] = pd.to_datetime(dataframe["time"],format="%H:%M:%S.%f")
output = (dataframe.set_index("time")
.tz_localize("utc")
.tz_convert("US/Eastern")
.reset_index()
)
output["time"] = output["time"].dt.time
>>> output
time val
0 15:13:12.349211 a
1 15:13:13.435233 b
2 15:13:14.345233 c
Convert dataframe column to datetime only if length of string is not zero
Two steps,
first lets create a series with your datetimes and coerce the bad values into NaTs
s = pd.to_datetime(data['etime'],errors='coerce',format='%Y%m%d%H%M%S')
second, lets find any values that aren't NaT
and replace them with your target formatting.
data.loc[~s.isna(),'etime'] = s.dt.strftime('%Y-%m-%d %H:%M')
day etime
0 15 2020-08-11 23:52
1 17
2 20 2020-08-11 21:52
3 14 20200811265205
4 25 2020-08-11 23:52
- assuming 26 is a typo in your hour column at index 3.
Related Topics
Are Global Variables Thread-Safe in Flask? How to Share Data Between Requests
Selenium "Selenium.Common.Exceptions.Nosuchelementexception" When Using Chrome
How to Import a Module Given the Full Path
Accessing Class Variables from a List Comprehension in the Class Definition
How to Concatenate Str and Int Objects
How to Add to the Pythonpath in Windows, So It Finds My Modules/Packages
Syntax Error on Print With Python 3
Remap Values in Pandas Column With a Dict, Preserve Nans
How to Split a String into a List
What Is the Purpose of the Single Underscore "_" Variable in Python
How to Use Multiprocessing Pool.Map With Multiple Arguments
What Does "Syntaxerror: Missing Parentheses in Call to 'Print'" Mean in Python