Read excel dates and convert them to individual strings [duplicate] - python

This question already has answers here:
Extract day and month from a datetime object
(4 answers)
Closed 12 months ago.
I recently started using python.
I have a series of dates in excel
01-05-2021
02-05-2021
.
.
29-05-2021
Now, I want to load this column and convert it into individual strings based on rows. So i can extract the day, month and year separately for each dates
Can someone help me how to do that??

you can do:
df = pd.read_excel("filename.xlsx")
# let's imagine your date column name is "date"
df["day"] = df["date"].apply(lambda elem:elem.split("-")[0])
df["month"] = df["date"].apply(lambda elem:elem.split("-")[1])
df["year"] = df["date"].apply(lambda elem:elem.split("-")[2])

from datetime import datetime
str_time = 01-05-2021
time_convert = datetime.strptime(str_time, '%d-%m-%Y')
print (time_convert, time_convert.day, time_convert.month, time_convert.year)
in your case, make the convert in looping for each data you got from the excel file

Related

How to add - to the cell that has dates [duplicate]

This question already has answers here:
How to change the datetime format in Pandas
(8 answers)
How can I format date time string which is not date time format so that I can use it with pd.to_datetime()?
(2 answers)
Convert DataFrame column type from string to datetime
(6 answers)
Closed 5 months ago.
I have a 'date' column that has a date value as 20170423 (yyyymmdd) how can i change it to 2017-04-23?
dataframe = df
column name 'date'
I read the below post, but none of the solutions worked
Fastest way to insert these dashes in python string?
Example
d = df['Date']
df['Date'] = '%s-%s-%s' % (d[:4], d[4:6], d[6:])
The output 0 0 20170711\n1 20170718\n2 20170718\n3... when i exported in .csv

How do I remove hours and seconds from my DataFrame column in python? [duplicate]

This question already has answers here:
How to change the datetime format in Pandas
(8 answers)
Keep only date part when using pandas.to_datetime
(13 answers)
Closed 10 months ago.
I have a DataFrame :
Age Gender Address Date
15 M 172 ST 2022-02-07 00:00:00
I Want to remove hh:mm:ss
I tried:
import datetime as dt
df["Date"]=df["Date"].dt.Date .
But I am receiving no change in date column format.
All I want is that the date column has only (YYYY-MM-DD).
You can use pd.to_datetime to convert Date column to datetime object.
df['Date'] = pd.to_datetime(df['Date']).dt.date
# or
df['Date'] = pd.to_datetime(df['Date']).dt.strftime('%Y-%m-%d')
# or
df['Date'] = df['Date'].str.split(' ').str[0]

Extrating data within database on the base of the datatime [duplicate]

This question already has answers here:
Select DataFrame rows between two dates
(13 answers)
Closed 2 months ago.
I have one dataset with several columns: id, Unixtime, X, Y, etc....
Unixtime is one sequence of date: 01-01-2010 00:00:00, 02-01-2010 00:00:00...etc up to 31-12-2021 23:59:59.
I would like to get data within specific range, between 01-01-2019 00:00:00 until 31-12-2019 23:59:59.
I tried with this script but I have had some problem, because I am not sure if python take all data.
import pandas as pd
data = pd.read_csv('name.csv', sep=';')
data.info()
start_time = '01-01-2010 00:00:00'
end_time = '31-12-2019 23:59:59'
mask =(data['Unixtime']>start_time)&(data['Unixtime']<=end_time]
x=data.loc[mask]
There is another solution?
f_date = data [(data['Unixtime Date'] < '23-03-21') & (data['Unixtime Date'] > '03-03-21')]

Why can't I make a column with extracted months from the 'dates' column in my DataFrame? [duplicate]

This question already has answers here:
Extracting just Month and Year separately from Pandas Datetime column
(13 answers)
Closed 3 years ago.
I have a dataframe with dates, and I want to make a column with only the month of the corresponding date in each row. First, I converted my dates to ts objects like this:
df['Date'] = pd.to_datetime(df['Date'])
After that, I tried to make my new column for the month like this:
df['Month'] = df['Date'].month
However, it gives me an error:
AttributeError: 'Series' object has no attribute 'month'
I do not understand why I can't do it like this. I double checked whether the conversion to ts objects actually works, and that does work. Also, if I extract 1 date using slicing, I can append .month to get the month. I technically could solve the problem by looping over all indices and then slicing for each index, but my dataframe contains 166000+ rows so that is not an option.
You have to use property (or accessor object) dt
df["month"] = df.date.dt.month

Why my code didn't select data from Pandas dataframe? [duplicate]

This question already has answers here:
How to filter by month, day, year with Pandas
(1 answer)
Keep only date part when using pandas.to_datetime
(13 answers)
Closed 4 years ago.
Why didn't my date filter work? All others filters work fine.
import pandas as pd
import datetime
data =pd.DataFrame({
'country': ['USA', 'USA', 'Belarus','Brazil'],
'time': ['2018-01-15 16:11:45.923570+00:00', '2018-01-15 16:19:45.923570+00:00', '2018-01-16 16:12:45.923570+00:00', '2018-01-17 16:14:45.923570+00:00']})
# Конвертируем в datetime
data['time'] = pd.to_datetime(data['time'])
# Конвертируем в date
data['time'] = data['time'].dt.date
print(data)
# Ищем дату '2018-12-12'
select_date = data.loc[data['time'] == '2018-01-17']
print(select_date)
How can I filter exact data from dataframe?
How can I iterate dataframe by date daily?
for i in data:
All rows in a specific day
I wish you all good luck and prosperity!
datetime.date objects are not vectorised with Pandas. The docs indicate this:
Returns numpy array of python datetime.date objects
Regular Python objects are stored in object dtype series which do not support fancy date indexing. Instead, you can normalize:
data['time'] = pd.to_datetime(data['time'])
select_date = data.loc[data['time'].dt.normalize() == '2018-01-17']
You can use the same idea to iterate your dataframe by day:
for day, day_df in data.groupby(data['time'].dt.normalize()):
# do something with day_df

Categories