I have a Pandas Dataframe that stores date in the format 19-Jul-18. I am trying to convert it to 2018-07-19
I tried doing pd.to_datetime(df['date']) but it dint help.
Related
Having a Pandas DataFrame with a column of TimeStamp yyyy-mm-dd HH:MM:SS timezone (e.g. 2020-06-01 04:26:00-05:00), how to extract new column with only yyyy-mm-dd HH
Tried:
df.index = df.Time.to_period(freq='T').index
Result in: yyyy-mm-dd HH:MM
you can use:
df['new_date']=df['your_date_columns'].dt.strftime('%Y-%m-%d %H')
I have a dataframe in pandas with some columns with dates in the following format
dates
202001
202002
I want to convert them to the following format
dates
2020-01-01
2020-02-01
Could anyone assist with converting the date format? Thanks
If need datetimes use to_datetime with format='%Y%m':
df['dates'] = pd.to_datetime(df['dates'], format='%Y%m')
You may use to_datetime here:
df["dates"] = pd.to_datetime(df["dates"] + '01', format='%Y%m%d', errors='ignore')
Note that your current text dates are year month only, so I concatenate 01 to the end of each one to form the first of the month, for each date.
Try this:
df['dates'] = df['dates'].astype(str)
df['dates'] = pd.to_datetime(df['dates'].str[:4] + ' ' + df['dates'].str[4:])
print(df)
Output:
dates
0 2020-01-01
1 2020-02-01
I have a timestamp column named time in pandas dataframe
a sample timestamp is
2021-01-17 18:11:23+00:00
and the column data type is
time datetime64[ns, psycopg2.tz.FixedOffsetTimezone...
now i am trying to convert the column to a list
df['time'].values.tolist(),
the above sample timestamp is now converted into epoch and show stored as
1610907083000000000
How can i tell pandas to store in iso string format rather than epoch
df['time'].dt.strftime('%Y-%m-%dT%H:%M:%S.%f%z').values.tolist()
You can change '%Y-%m-%dT%H:%M:%S.%f%z' with the format you like the most (I used the ISO 8601 format; see here for the format specs)
I have two columns in a pandas dataframe that have datetimes loaded from a Postgresql database. In each column there are datetimes with varying timezones. How can I convert these to datetimes to be of the same timezone?
Assuming the columns are timezone aware, you can use
for column in df.columns:
df[column] = pd.DatetimeIndex(df[column]).tz_convert(tz)
where tz is the time zone you want.
After loading data from a csv file, I set the index to the "Date" column and then convert the index to datetime.
df1=pd.read_csv('Data.csv')
df1=df1.set_index('Date')
df1.index=pd.to_datetime(df1.index)
However after conversion the datetime format shows it has been misinterpreted:
original date was e.g. 01-10-2014 00:00:00
but Pandas converts it to 2014-01-10 00:00:00
How can I get Pandas to respect or recognize the original date format?
Thank you
Your datestrings were being interpreted as month first, you need to specify the correct format:
df1.index=pd.to_datetime(df1.index, format='%d-%m-%Y %H:%M:%S')
so that it doesn't interpret the first part as the month
In [128]:
pd.to_datetime('01-10-2014 00:00:00', format='%d-%m-%Y %H:%M:%S')
Out[128]:
Timestamp('2014-10-01 00:00:00')