Convert index to DateTime - python

I have an excel file with data. I defined this file as a DataFrame (5000,12) using python/pandas. As an index, I set the date based on the below:
Data_Final=Data.set_index(['Date Time']) # Data_Final is Dataframe
For example, the first index is 01/01/2016 00:00. Now I want this index in datetime. How is this conversion done?

use the .to_datetime() method
Data_Final = Data
Data_Final['Date Time'] = pd.to_datetime(Data['Date Time'])
Data_Final.set_index('Date Time', inplace=True)
How to convert string to datetime format in pandas python?

Related

Removing time from a date column in pandas

I have pandas data frame that had a Date (string) which i could convert and set it up as a index using the set_index and to_datetime functions
usd2inr_df.set_index(pd.to_datetime(usd2inr_df['Date']), inplace=True)
but the resulting dataframe has the time portion which i wanted to remove ...
2023-02-14 00:00:00
I wanted to have it as 2023-02-14
How do i setup the call such that, i can get have the date without the time portion as a index on my dataframe
usd2inr_df['Date'] = pd.to_datetime(usd2inr_df['Date']).dt.normalize()
usd2inr_df.set_index(usd2inr_df['date'])
Using the .to_datetime() method, converts a Series to a pandas datetime object.
Using the Series.dt.date, returns a 'yyyy-mm-dd' date form.
Using the DataFrame.index, sets the index of the dataFrame.
import pandas as pd
# create a dataFrame as an example
df = pd.DataFrame({'Name': ['Example'],'Date': ['2023-02-14 10:01:11']})
print(df)
# convert 'yyyy-mm-dd hh:mm:ss' to 'yyyy-mm-dd'.
df['Date'] = pd.to_datetime(df['Date']).dt.date
# set 'Date' as index
df.index = df['Date']
print(df)
Output
Name Date
0 Example 2023-02-14 10:01:11
-------------------------------------------------------
Name Date
Date
2023-02-14 Example 2023-02-14

pd.read_csv only parsing one out of two column dates

I'm trying to read data from a csv and parsing dates but I found this issue in which only one of the columns gets a datetime format and the other one still remains an object.
dtype = {'col1':'category','col2':'category','Start Date':'str','End Date':'str'}
dates = ['col3','col4']
df = pd.read_csv(filepath,dtype=dtype,parse_dates=dates,dayfirst=False)
Both date columns have same format.
when I do df.info() I get the following:
df.info() output
I tried using dayfirst input and the formatter but it didn't help.
I expect that both columns in the list would get datetime object but for some reason they aren't.
Update: tried to recreate a minimal reproducible data set by doing the code block below but this is behaving as expected, producing both Start Date and End Date columns as datetime.
import pandas as pd
df = pd.DataFrame({'col1':['ABC','ABC','DCF','DCF'],
'Start Date':['12-31-2022','12-31-2022','12-31-2022','12-31-2022'],
'End Date':['12-31-2023','12-31-2023','12-31-2023','12-31-2023']
})
df.to_csv('test.csv',index=0)
df2 = pd.read_csv('test.csv',
dtype={'col1':'category','Start Date':'str','End Date':'str'},
parse_dates = ['Start Date','End Date'],
dayfirst=False
)
df2.info()

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

Combining separate columns of time and date to one in pandas

I have data in excel like this
I want to combine columns of Date and Time using the following code
import pandas
df = pd.read_excel('selfmade.xlsx')
df['new'] = df['Date'].map(str) + df['Time'].map(str)
print(df)
but it prints the results like this.
I want the last column in format like 2016-06-14 10:00:00
What should I change in my code to get the desired results
I think you need to_datetime and to_timedelta, also is necessary convert Time column to string by astype:
df['new'] = pd.to_datetime(df['Date']) + pd.to_timedelta(df['Time'].astype(str))
If dtype of Date column is already datetime:
df['new'] = df['Date'] + pd.to_timedelta(df['Time'].astype(str))

Categories

Resources