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

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')]

Related

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]

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

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

How to change the date format d-m-y to dd-mm-yyyy using pandas [duplicate]

This question already has answers here:
How to change the datetime format in Pandas
(8 answers)
Closed 2 months ago.
I am trying to change the date format reported in the header. where the date formats reported are as the below
1-2-22 thru 1-8-22,
1-16-22 thru 1-22-22
need to update the date formats to dd-mm-yyyy in the header
df['Date'].dt.strftime('%d/%m/%Y')
import pandas as pd
list_of_dates = ['2018-10-26 12:00', '2018-10-26 12:00', '2018-10-26 12:00']
dates_df = pd.DataFrame(list_of_dates, columns=['date'])
dates_df['date'] = pd.to_datetime(dates_df['date'])
dates_df['date']= dates_df['date'].dt.strftime('%m/%d/%Y')
print(dates_df)
# date
# 0 10/26/2018
# 1 10/26/2018
# 2 10/26/2018

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

How to use Pandas calculate # of weeks/days? [duplicate]

This question already has answers here:
Pandas Datetime: Calculate Number of Weeks Between Dates in Two Columns
(2 answers)
Closed 4 years ago.
Hi, I have a dataframe with date columns. I want to add a column to calculate how many weeks since the contact? For example, today's date is 20-Sep-18, and use this date to calculate with the column.
Can anyone help me with this questions? Thanks!
You can do like this.
df['Contact Date']= pd.to_datetime(df['Contact Date'])
import datetime
df['How Many days'] = datetime.datetime.now() - df['Contact Date']

Categories