I want to transform a date which has the following format "2022-03-17T19:38:48.331000Z"
in order to know if it would give me valuable information.
import numpy as np
import pandas as pd
import requests, json
from pandas import json_normalize
from datetime import datetime
from datetime import timezone
!pip3 install zulu
input: column_timestamp
id timestamp
ed25291d0f5edd91615d154f243f82f9 2022-03-18T07:33:36.882000Z
e02c5db9e6f6fca078798c9b2d486a81 2022-03-18T07:33:36.945000Z
f8756b6af18c2fedd8a295040279aecc 2022-03-18T07:33:37.549000Z
...
from datetime import datetime
from datetime import timezone
!pip3 install zulu
time = []
for i in range(505):
dt = zulu.parse(column_timestamp["timestamp"][i])
dt.format('% m/% d/% y % H:% M:% S % z')
time.append(dt)
i = +1
time_df = pd.DataFrame(time)
time_df
output:
0
0 2022-03-18 07:33:36.882000+00:00
1 2022-03-18 07:33:36.945000+00:00
2 2022-03-18 07:33:37.549000+00:00
3 2022-03-18 07:33:37.550000+00:00
4 2022-03-18 07:33:37.552000+00:00
... ...
I want to know if it's correct and as well split this dataframe into different columns:
Date
Hour
Minute
Seconds
And make sure if I'm doing the conversion correct:
'2022-03-18T07:33:36.746000Z'
Related
so I want to integrate my code with python API
# Install required library
!pip install xlrd
import pandas as pd
from datetime import time, timedelta, datetime
import openpyxl
import math
!pip install pytanggalmerah
from pytanggalmerah import TanggalMerah
# Mount google drive
from google.colab import drive
drive.mount('/content/drive')
# Read the Excel file
path = '/content/drive/MyDrive/Colab Notebooks/Book2.xls'
df = pd.read_excel(path)
# Convert the 'Tgl/Waktu' column to datetime format
df['Tgl/Waktu'] = pd.to_datetime(df['Tgl/Waktu'])
# Extract the date and time from the 'Tgl/Waktu' column
df['Date'] = df['Tgl/Waktu'].dt.date
a = df['Date'].drop_duplicates()
print(a)
with that code, it will have output as
0 2022-12-17
2 2022-12-19
4 2022-12-20
6 2022-12-21
8 2022-12-22
10 2022-12-23
Name: Date, dtype: object
and for the API i will use pytanggalmerah which will need the input to be
t.set_date("2019", "02", "05") #the order is Year, Month, Date
t.check()
how do i change my date object into string then make a for loop with my string to check whether is it true or false
how do i do it? how to integrate it?
You can use a list comp:
dates = [(str(x.year), str(x.month), str(x.day)) for x in df["Tgl/Waktu"].unique().tolist()]
for date in dates:
year, month, day = date
t.set_date(year, month, day)
is_holiday question:
import numpy as np
holidays = pd.DataFrame(holiday_data).rename(columns={"Date": "Day"})
cols = ["Year", "Month", "Day"]
holidays = holidays.assign(Date=pd.to_datetime(holidays[cols]).dt.date).drop(columns=cols)
df["is_holiday"] = np.where(df["Tgl/Waktu"].isin(holidays["Date"].to_list()), True, False)
print(df)
I have a csv file like this:
Tarih, Şimdi, Açılış, Yüksek, Düşük, Hac., Fark %
31.05.2022, 8,28, 8,25, 8,38, 8,23, 108,84M, 0,61%
(more than a thousand lines)
I want to change it like this:
Tarih, Şimdi, Açılış, Yüksek, Düşük, Hac., Fark %
5/31/2022, 8.28, 8.25, 8.38, 8.23, 108.84M, 0.61%
Especially "Date" format is Day.Month.Year and I need to put it in Month/Day/Year format.
i write the code like this:
import pandas as pd
import numpy as np
import datetime
data=pd.read_csv("qwe.csv", encoding= 'utf-8')
df.Tarih=df.Tarih.str.replace(".","/")
df.Şimdi=df.Şimdi.str.replace(",",".")
df.Açılış=df.Açılış.str.replace(",",".")
df.Yüksek=df.Yüksek.str.replace(",",".")
df.Düşük=df.Düşük.str.replace(",",".")
for i in df['Tarih']:
q = 1
datetime_obj = datetime.datetime.strptime(i, "%d/%m/%Y")
df['Tarih'].loc[df['Tarih'].values == q] = datetime_obj
But the "for" loop in my code doesn't work. I need help on this. Thank you
Just looking at converting the date, you can import to a datetime object with arguments for pd.read_csv, then convert to your desired format by applying strftime to each entry.
If I have the following tmp.csv:
date, value
30.05.2022, 4.2
31.05.2022, 42
01.06.2022, 420
import pandas as pd
df = pd.read_csv('tmp.csv', parse_dates=['date'], dayfirst=True)
df['date'] = df['date'].dt.strftime('%m/%d/%Y')
print(df)
output:
date value
0 05/30/2022 4.2
1 05/31/2022 42.0
2 06/01/2022 420.0
The problem:
I am trying to print out the date from a month ago. So instead of the result being:
>>> 2021-03-12
It will be in this instead
>>> 2021-02-12
Here is my code:
from datetime import date
import datetime
from email.utils import formatdate
now = formatdate(timeval=None, localtime=False, usegmt=True)
tday = date.today()
print(tday)
I have seen tons of different examples but all of them change the format of the date structure that I already have.
from datetime import datetime
from dateutil.relativedelta import relativedelta
now = datetime.now()
last_month_date = now + relativedelta(months=-1)
last_month_date=last_month_date.split(" ")[0]
Use dateutil as it has a improved delta
Add to #chess_lover_6
from datetime import datetime
from dateutil.relativedelta import relativedelta
now = datetime.now()
last_month_date = now + relativedelta(months=-1)
last_month_date.strftime('%Y-%m-%d')
You will get 2021-02-12
I am using the following code to generate data series :-
import pandas as pd
import warnings
warnings.filterwarnings("ignore")
import numpy as np
import calendar
from datetime import datetime
from itertools import cycle, islice
month_input = "Jan"
year_input = 2018
month_start= str(month_input)
year_start = int(year_input)
start = pd.to_datetime(f'{month_start}{year_start}', format='%b%Y')
end = pd.to_datetime(f'{month_input}{year_start + 1}', format='%b%Y') - pd.Timedelta('1d') # Generating Date Range for an Year
daily_series_cal = pd.DataFrame({'Date': pd.date_range(start, end)})
When I am trying to do:
print(daily_series_cal["Date"][0])
It is giving as output as :-
2018-01-01 00:00:00
How can I change the format of whole column to 01/01/2018 ie mm/dd/yyyy?
It is possible by DatetimeIndex.strftime, but lost datetimes and get strings:
daily_series_cal = pd.DataFrame({'Date': pd.date_range(start, end).strftime('%m/%d/%Y')})
I have a csv with data like this:
[id names timestamp is_valid]
[1 name:surname 2016-06-09 23:29:50.083093 True]
I need to select rows based on this condition: if is_valid is true and if timestamp has passed 24 hours. So it should be True and current time 2016-06-10 23:29:50.083093 to pass the condition.
How can I achieve this? I know how to apply the first condition:
from datetime import datetime, timedelta
import pandas as pd
from dateutil import parser
df=pd.read_csv('acc.csv')
user=(df[df['is_valid']==True])
I can even print timestamp, parse it and compare with datetime.now(). But this is definitely a terrible thing to do.
try this:
from datetime import datetime, timedelta
import pandas as pd
from dateutil import parser
df = pd.read_csv('acc.csv')
tidx = pd.to_datetime(df['timestamp'].values)
past_24 = (pd.datetime.now() - tidx).total_seconds() > 60 * 60 * 24
user = df[df['is_valid'] & past_24]