How to change date format in dataframe - python

I'm trying to calculate the beta in stock but when I bring in the data it has a time in the date frame how can I drop it?

If you want to transform a datetime object to a date object, you can get the date with the .date on the index, then just reassign it:
Ford_df.index = Ford_df.index.date
If instead you want the index to be a string with your custom format (%Y-%m in this example) then do:
Ford_df.index = Ford_df.index.strftime("%Y-%m")
Both solutions presume your index is a DatetimeIndex. If it is not you can transform it with:
Ford_df.index = pd.to_datetime(Ford_df.index)

Related

Change dtype of column from object to datetime64

I have a column with dates (Format: 2022-05-15) with the current dtype: object. I want to change the dtype to datetime with the following code:
df['column'] = pd.to_datetime(df['column'])
I receive the error:
ParserError: Unknown string format: DU2999
Im changing multible columns (e.g. another date column with format dd-mm-yyyy hh-mm-ss). I get the error only for the mentioned column.
Thank you very much for your help in advance.
If you want to handle this error by setting the resulting datetime value to NaT whenever the input value is "DU2999" (or another string that does not match the expected format), you can use:
df['column'] = pd.to_datetime(df['column'], errors='coerce'). See https://pandas.pydata.org/docs/reference/api/pandas.to_datetime.html.
If you want to manually correct this specific case, you could use print(df.loc[df['column']=="DU2999"]) to view that row of the dataframe and decide what to overwrite it with.
As #Naveed said there invalid date strings in date column such as DU2999 . What you can do is simply find find out which strings that are not in date format.
temp_date = pd.to_datetime(df['Date_column'], errors='coerce', dayfirst=True)
mask = temp_date.isna()
out = df[mask]
#Problmeatic columns ==Filter columns with True values
df_problematic = out[ out.any(axis=1)]
print(df_problematic)

How do I import a column as datetime.date?

I have a dataset in CSV which first column are dates (not datetimes, just dates).
The CSV is like this:
date,text
2005-01-01,"FOO-BAR-1"
2005-01-02,"FOO-BAR-2"
If I do this:
df = pd.read_csv('mycsv.csv')
I get:
print(df.dtypes)
date object
text object
dtype: object
How can I get column date by datetime.date?
Use:
df = pd.read_csv('mycsv.csv', parse_dates=[0])
This way the initial column will be of native pandasonic datetime type,
which is used in Pandas much more often than pythonic datetime.date.
It is a more natural approach than conversion of the column in question
after you read the DataFrame.
You can use pd.to_datetime function available in pandas.
For example in a dataset about scores of a cricket match. I can convert the Matchdate column to datatime object by applying pd.to_datetime function based on the data time format given in the data. ( Refer https://www.w3schools.com/python/python_datetime.asp to assign commands based on your data time formating )
cricket["MatchDate"]=pd.to_datetime(cricket["MatchDate"], format= "%m-%d-%Y")

Creating Datetime index in python

I am trying to create datetime index in python. I have an existing dataframe with date column (CrimeDate), here is a snapshot of it:
The date is not in datetime format though.
I intent to have an output similar to the below format, but with my existing dataframe's date column-
The Crimedate column has approx. 334192 rows and start date from 2021-04-24 to 1963-10-30 (all are in sequence of months and year)
First you'll need to convert the date column to datetime:
df['CrimeDate'] = pd.to_datetime(df['CrimeDate'])
And after that set that column as the index:
df.set_index(['CrimeDate'], inplace=True)
Once set, you can access the datetime index directly:
df.index

Convert to datetime using column position/number in python pandas

Very simple query but did not find the answer on google.
df with timestamp in date column
Date
22/11/2019 22:30:10 etc. say which is of the form object on doing df.dtype()
Code:
df['Date']=pd.to_datetime(df['Date']).dt.date
Now I want the date to be converted to datetime using column number rather than column name. Column number in this case will be 0(I have very big column names and similar multipe files, so I want to change date column to datetime using its position '0' in this case).
Can anyone help?
Use DataFrame.iloc for column (Series) by position:
df.iloc[:, 0] = pd.to_datetime(df.iloc[:, 0]).dt.date
Or is also possible extract column name by indexing:
df[df.columns[0]] = pd.to_datetime(df[df.columns[0]]).dt.date

Converting dtype: period[M] to string format

I have converted my dates in to an Dtype M format as I don't want anything to do with the dates. Unfortunately I cannot plot with this format so I want now convert this in to strings.
So I need to group my data so I can print out some graphs by months.
But I keep getting a serial JSON error when my data is in dtype:Mperiod
so I want to convert it to strings.
df['Date_Modified'] = pd.to_datetime(df['Collection_End_Date']).dt.to_period('M')
#Add a new column called Date Modified to show just month and year
df = df.groupby(["Date_Modified", "Entity"]).sum().reset_index()
#Group the data frame by the new column and then Company and sum the values
df["Date_Modified"].index = df["Date_Modified"].index.strftime('%Y-%m')
It returns a string of numbers, but I need it to return a string output.
Use Series.dt.strftime for set Series to strings in last step:
df["Date_Modified"]= df["Date_Modified"].dt.strftime('%Y-%m')
Or set it before groupby, then converting to month period is not necessary:
df['Date_Modified'] = pd.to_datetime(df['Collection_End_Date']).dt.strftime('%Y-%m')
df = df.groupby(["Date_Modified", "Entity"]).sum().reset_index()

Categories

Resources