How to achieve different date formats for the cell and fx field in Excel using python?
I have tried num_format, but that is not working
Screenshot of how I want the date to be
I'm new to Python and dataframes.
I have a date value that is not formatted as date. Since this value has a 'weird' format, pandas' function to_datetime() doesn't work properly. The values are formatted like:
['20190630', '20190103']
This is the 'yyyymmdd' format.
I have tried to slice the values and make different columns where I extract the year- month- day. But this doesn't work, since the slicing wasn't working. This is the code I have now, but it isn't doing anything.
df.Date = pd.to_datetime(df.date)
I would like to have the dd-mm-yyyy format and datetime type. What can I do?
I have imported some data but the date column is in this format: 50:58.0, 23:11.0.. etc- when i click on the cell in excel however it is: 02/05/2019 07:50:58 (for the first one 50:54.0). So when i import into python as a pandas table it still retains the 50:54.0 format although i do not know why.
I tried changing the column to datetime as:
df['EventTS'] = pd.to_datetime(df['EventTS'], format='%d%b%Y:%H:%M:%S.%f')
but it doesn't work the error is time data '07:27.0' does not match format '%d%b%Y:%H:%M:%S.%f' (match)
without changing format in excel how do i correct this issue in python?
I am using the Google Sheets API for inserting some data on my sheet.
Every row has a date cell that has values in this format "2017-02-01 19:33:56+00:00" I would like it to display it in this format (02 January 2017) but I would like to have the original format hidden on the same cell.
Is there a script or something that I can use to display the date in that format?
Just add a custom numberformat from the format menu. This one should do the trick "dd MMM yyyy". The date doesn't change so if you want to go back to the old one you always can. You can use a script to switch between them using range.setNumberFormat
You can use this script to setNumberFormats():
SpreadsheetApp.getActiveRange().setNumberFormat("dd MMM yyyy");
I not familiar with the ss+00:00 not sure what that means. But perhaps you can checked the documentation below. You might be able to use range.getNumberFormat() to get and save that format for future use.
var formatString=SpreadsheetApp.getActiveRange().getNumberFormat();
Number Formats Documentation
This format "2017-02-01 19:33:56+00:00" is not being recognized for Google Sheets, it's because it has an additional value to the final that indicates the timezone of the date "+00:00", i decided to create an small script for getting ride of that timezone value and also add/sub hours and minutes to the date so i can see the correct hour in my timezone.
from datetime import datetime, timedelta
def formatDatetime(o_date, f_hours=0, f_minutes=0):
# ----- Removing the timezone from the data str
date_a = o_date.split('+')
date = date_a[0]
#----------------------------------------------------
# ----- Format/Add/Sub Hours and minutes
date = datetime.strptime(date, '%Y-%m-%d %H:%M:%S')
date = date + timedelta(hours=f_hours)
date = date + timedelta(minutes=f_minutes)
date_f = format(date, '%m/%d/%Y %H:%M:%S')
# -------------------
return date_f
Now google sheet recognizes this value as a date, so i can make a custom formatting for showing a nice date in my worksheet.
I am trying to format the date-time column to the given format. The only solution I came across in Python is via using 'strftime' which convert the date-time to unicode.
original = df.date
required_date_format ='%d/%m/%Y'
formatted = df.date.dt.strftime(required_date_format)
converted_after_formatting = pd.to_datetime(\
df.date.dt.strftime(required_date_format),\
format=required_date_format)
The outputs of the above code snippet are as follows:
I want a solution that can format the date-time column without loosing it's data-type.