So I got my dataframe from a JSON file, and the date is labelled as 2000M01 for 2000 january, 2000M02 for 2000 february etc. I need to have it in a different format: 2000Jan, 2000Feb etc.-I have a different data set in this format, I could bring both of these to a third one, if that's easier. Like 2000-01 or some official date format.
My main issue is that as far as I know 2000M01 is not an official data format in any way, so I can't just convert it that way.
Any ideas how I could convert this?
You can easily feed a custom format to pd.to_datetime, in your case it would be '%YM%m', e.g.:
pd.to_datetime('2000M01', format = '%YM%m')
Then you can convert it to any format you want.
You can change the date format with the datetime module
def reformat_date(date_from_json):
date = datetime.datetime.strptime(date_from_json, "%YM%m")
return date.strftime("%Y%b")
As specified in datetime documentation in strftime and strptime formats, you can deal with the unusual date formatting with %YM%m dealing with the input format with the day defaulting to the 1st, and %Y%b giving you the format you want.
Then you map the function to the pandas dataframe
dataframe['DATE_COLUMN'] = dataframe['OLD_DATE_COLUMN'].map(lambda date: reformat_date(date))
Related
I have a csv file where the a timestamp column is coming with values in the following format: 2022-05-12T07:09:33.727-07:00
When I try something like:
df['timestamp'] = pd.to_datetime(df['timestamp'])
It seems to fail silently as the dtype of that column is still object. I am wondering how I can parse such a value.
Also, what is the strategy so that it remains robust to a variety of input time formats?
I'a m reading a csv file with Pandas. In the file there is a column with dates in dd/mm/yyyy format.
def load_csv():
mydateparser = lambda x: dt.datetime.strptime(x, "%d/%m/%Y")
return pd.read_csv('myfile.csv', delimiter=';', parse_dates=['data'], date_parser=mydateparser)
Using this parser the column 'data' type becomes data datetime64[ns], but the format is changed to yyyy-mm-dd.
I need the the column 'data' type to be datetime64[ns] and formated as dd/mm/yyyy.
How can it be done?
Regards,
Elio Fernandes
Date is not stored in yyyy-mm-dd format or dd/mm/yyyy format. It's stored in datetime format. Python by default chooses to shows it in yyyy-mm-dd format. But don't get it wrong, it still is stored in datetime format.
You will get a better idea if you add time to data and then try to display it.
The way to achieve what you wish is by changing date to string right before displaying, so as, it remains datetime in dataframe but you get the specified string format when you display.
The following uses Series.strftime() to change to string. Documentation here.
df['data'].strftime('%d/%m/%Y')
or
The following uses datetime.strftime() to change to string. Documentation here.
df['data'].apply(lambda x: x.strftime('%Y-%m-%d'))
For further reference check out strftime-and-strptime-behavior.
This question will be of great help to understand how datetime is stored in python:
How does Python store datetime internally?
I have the checkout column in Dataframe of type 'object' in '2017-08-04T23:31:19.000+02:00' format.
But i want it in the format as shown in the image.
Can anyone help me please.
Thank you:)
You should be able to convert the object column to a date time column, then use the built in date and time functions.
# create an intermediate column that we won't store on the DataFrame
checkout_as_datetime = pd.to_datetime(df['checkout'])
# Add the desired columns to the dataframe
df['checkout_date'] = checkout_as_datetime.dt.date
df['checkout_time'] = checkout_as_datetime.dt.time
Though, if you're goal isn't to write these specific new columns out somewhere, but to use them for other calculations, it may be simpler to just overwrite your original column and use the datetime methods from there.
df['checkout'] = pd.to_datetime(df['checkout'])
df['checkout'].dt.date # to access the date
I haven't tested this, but something along the lines of:
df['CheckOut_date'] = pd.to_datetime(df["CheckOut_date"].dt.strftime('%Y-%m-%d'))
df['CheckOut_time'] = pd.to_datetime(df["CheckOut_time"].dt.strftime('%H:%m:%s'))
Is possible change format date by specific pattern ? I need to made a function which has a two parameters. First is date and second is pattern. I need convert more date variants. Goal this function is change US and European date format.
For example i need convert
EU: dd:MM:yyyy hh:mm:ss
to
US: MM:dd:yyyy hh:mm:ss
On another page i need change
EU: dd/MM/yyyy
to
US: MM/dd/yyyy
And i have a several next variant to convert
And i want to made a similar function
Formater(euDate, pattern)
BEGIN
....
RETURN usDate
My production server is unfortunately SQL server 2005 and doesn't support function FORMAT(). And function CONVERT() doesn't support some variant of date, which i need convert. So in my current solution i parse EU date at individualy parts (#day = day(#euDate), #month, #year, ...) and join them in new string . And i compare it with input parameter in pattern and return CASE which is equal like pattern. I want to this function make general and simplier.
Thank you for Your advice.
You almost certainly can use the convert function. You can read more about all the options here.
If there is some obscure invariant you need, check out this blog by Anubhav Goyal.
I got a date of the type SYDATUM and wondering how to format it in a format like m/d/y.
I've found some snippets on the web, but they were not really helpful.
-thanks yor your help.
You should be more specific - what exactly do you want to do with the date (use type D internally, it's shorter and does the same thing).
Do you want to WRITE it to a list screen? Use the formatting options described in the documentation and online help:
WRITE l_my_date MM/DD/YYYY.
Do you want to convert it to a text variable? Very similar:
WRITE l_my_date TO l_my_text MM/DD/YYYY.
To set the date format in a SAPscript form, see the SET DATE MASK command.
To print the formatted date in a SmartForm, use the WRITE command and a temporary variable (yes, ugly, I know...)
Most controls (ALV Grid for example) should take care of the format automatically.
However - be careful when hard-coding the format into your application. Usually you don't have to do this because the system automatically uses the format specified in the user master data. This ensures that every user will see the date formatted according to their locale settings.
Normally it's better to export the date into the plant level country specific date format:
if w_country is initial.
select single LAND1
from T001W
into w_country
where WERKS eq w_the_plant.
endif.
SET COUNTRY w_country.
write w_the_date to w_export.
for example 03/04/2002 could be different date in different country.
You can try the keyword TRANSLATE. Alternatively suggest you could have a look at this link