I have a DataFrame with a column containing timestamps and I would like to convert the column to date time in Python and save the file with a column containing the date and time. Here is the code:
import pandas as pd
df = pd.DataFrame({
"time": [1465585763000, 1465586363000, 1465586963000,
1465587563000, 1465588163000]})
df
This could also work
import pandas as pd
from datetime import datetime as dt
d = {'time': [1465585763000, 1465586363000, 1465586963000,
1465587563000, 1465588163000]}
print(d['time'])
new = [dt.fromtimestamp(x/1000).strftime('%Y-%m-%d %H:%M:%S') for x in d['time']]
pd.to_datetime(new)
This could work
from datetime import datetime as dt
import pandas as pd
times = [
1465585763000,
1465586363000,
1465586963000,
1465587563000,
1465588163000]
start_ts = dt.timestamp(dt(1970, 1, 1))
dates = [dt.fromtimestamp(time / 1000 + start_ts) for time in times]
pd.to_datetime(dates)
Related
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
In the code below, I am trying to get data for a specified date only.
It perfectly works for the shown code.
But if I change the date to 26-12-2020, it results in data of both 26-12-2020 and 27-12-2020.
import csv
import datetime
import os
import pandas as pd
import xlsxwriter
import numpy as np
from datetime import date
import datetime
import calendar
rdate = 27-12-2020
data= pd.read_excel(r'C:/Clover Workspace/NPS/Customer Feedback-28-12-2020.xlsx')
data.drop(columns=['User ID','Comments','Purpose ID'],inplace= True, axis=1)
df = pd.DataFrame(data, columns=['Name','Rating','Date','Store','Feedback choice'])
df['Date'] = pd.to_datetime(data['Date'])
df= df[df['Date'].ge("27-12-2020")]
How can I generate the output only for the specified date, irrespective of the date on the excel sheet name?
here:
df= df[df['Date'].ge("27-12-2020")]
.ge means greater or equal, so when you put in 26-12-2020 you get both days. Try using .eq instead:
df= df[df['Date'].eq("26-12-2020")]
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 am very new python and ml please give me solution.
query = "Select created_date,device_data From smeonix.uplink_data"
result = session.execute(query, timeout=None)
row = result._current_rows
uplink_data = pd.Series(row['device_data'].values,
index = pd.DatetimeIndex(data =
(tuple(pd.date_range('31/12/2018 00:00:00',
periods = 81389,
freq = 'A-DEC'))),
freq = 'A-DEC'))
this is how we can print the date range by using the panda Dataframe:
import pandas as pd
from datetime import datetime
import numpy as np
date_rng = pd.date_range(start='1/1/2018', end='1/08/2018', freq='H')
You should read the documentation; your arguments to date_range are wrong.
I'm trying to create the following date range series with quarterly frequency:
import pandas as pd
pd.date_range(start = "1980", periods = 5, freq = 'Q-Dec')
Which returns
DatetimeIndex(['1980-03-31', '1980-06-30', '1980-09-30', '1980-12-31',
'1981-03-31'],
dtype='datetime64[ns]', freq='Q-DEC')
However, when I output this object to CSV I see the time portion of the series. e.g.
1980-03-31 00:00:00, 1980-06-30 00:00:00
Any idea how I can get rid of the time portion so when I export to a csv it just shows:
1980-03-31, 1980-06-30
You are not seeing nano-seconds, you are just seeing the time portion of the datetime that pandas has created for the DatetimeIndex. You can extract only the date portion with .date().
Code:
import pandas as pd
dr = pd.date_range(start="1980", periods=5, freq='Q-Dec')
print(dr[0].date())
Results:
1980-03-31