to change any form of date string using pandas - pandas

my date time format in excel is 01-12-2010 08:26 (date =01,month =12) when i import that into pandas and change dtype to datetime, month and date both get swapped.I am new to this please help
Output of pandas is
x .date
12
x. month
1
Excel
Invoice date = 01/12/2010 08:26
PANDAS
When import using sales = pd.read_csv()
sales["InvoiceDate"] = sales["InvoiceDate"].astype("datetime64[ns]")
[ln] y["InvoiceDate"].loc[0].
[Out] Timestamp['2010-01-12 08:26:00']
[ln] y["InvoiceDate"].loc[0].day
[out] 12
the output of this should be 1 instead of 12
where i am getting it wrong
please help

you can use pd.to_datetime with parameter dayfirst like below
pd.to_datetime("01/12/2010 08:26", dayfirst=True)

Related

How can convert string to date which only contains year number?

Create a dataframe whose first column is a text.
import pandas as pd
values = {'dates': ['2019','2020','2021'],
'price': [11,12,13]
}
df = pd.DataFrame(values, columns = ['dates','price'])
Check the dtypes:
df.dtypes
dates object
price int64
dtype: object
Convert type in the column dates to type dates.
df['dates'] = pd.to_datetime(df['dates'], format='%Y')
df
dates price
0 2019-01-01 11
1 2020-01-01 12
2 2021-01-01 13
I want to convert the type in dates column to date and the dates in the following format----contains only year number:
dates price
0 2019 11
1 2020 12
2 2021 13
How can achieve the target?
If you choose to have the datetime format for your columns, it is likely to benefit from it. What you see in the column ("2019-01-01") is a representation of the datetime object. The realquestion here is, why do you need to have a datetime object?
Actually, I don't care about datetime type:
Use a string ('2019'), or preferentially an integer (2019) which will enable you to perform sorting, calculations, etc.
I need the datetime type but I really want to see only the year:
Use style to format your column while retaining the underlying type:
df.style.format({'dates': lambda t: t.strftime('%Y')})
This will allow you to keep the type while having a clean visual format

Calculate number of days left from nearest date in Pandas

I have a list like this:
dates = ["2020-05-08","2019-02-22", "2014-08-16"...........]
And a DF like this:
date .....
2020-12-19 .....
2015-06-01 .....
2018-03-06 ....
......
I want to create another column named "daysLeft", which count, the days from the nearest date.
For example.
If today is 24th Dec, then "1" day is left for chrismas. But if today is 26th Dec, "-1" day is left for Christmas. (Subtract date with nearest date)
I am not sure in this answers your question, but this might be a step in the right direction:
#This Function Converts String to DateTime Object for Date Manipulation
from datetime import datetime
def make_date(any_value):
return datetime.strptime(any_value, '%Y-%m-%d')
#We apply thr Function to the list
dates = ["2020-05-08","2019-02-22", "2014-08-16"]
dt_obj = list(map(make_date, dates))
#We apply the function to the DataFrame
date_df_list=["2020-12-19", "2015-06-01", "2018-03-06" ]
import pandas
date_df=pandas.DataFrame(date_df_list, columns=["date"])
date_df['date'] = date_df['date'].astype(str) #Each object needs to be converted to String for Function
date_df_yyyymmdd = pandas.DataFrame(columns=['date']) #Initialise Empty DataFrame
date_df_yyyymmdd['date'] = date_df['date'].apply(make_date)
#In this example we Find Difference in Dates for the first date of list
#Similarly we can find for all the other dates in the list
date_df['daysLeft'] = date_df_yyyymmdd['date'].apply(lambda x: (x-dt_obj[1]).days)
print(date_df)

convert pandas datetime64[ns] to julian day

I am confused by the number of data type conversions and seemingly very different solutions to this, none of which I can get to work.
What is the best way to convert a pandas datetime column (datetime64[ns] eg 2017-01-01 03:15:00) to another column in the same pandas dataframe, converted to julian day eg 2458971.8234259?
Many thanks
Create DatetimeIndex and convert to julian dates:
df = pd.DataFrame({'dates':['2017-01-01 03:15:00','2017-01-01 03:15:00']})
df['dates'] = pd.to_datetime(df['dates'])
df['jul1'] = pd.DatetimeIndex(df['dates']).to_julian_date()
#if need remove times
df['jul2'] = pd.DatetimeIndex(df['dates']).floor('d').to_julian_date()
print (df)
dates jul1 jul2
0 2017-01-01 03:15:00 2.457755e+06 2457754.5
1 2017-01-01 03:15:00 2.457755e+06 2457754.5
Because:
df['jul'] = df['dates'].dt.to_julian_date()
AttributeError: 'DatetimeProperties' object has no attribute 'to_julian_date'

pandas string to date type conversion in proper format

I am getting date data in string format in pandas like 10-Oct,11-Oct but i want to make it date data type like this format 2019-10-10,2019-10-11
is there any easy way available in pandas?
Use to_datetime with added year and parameter format:
df = pd.DataFrame({'date':['10-Oct', '11-Oct']})
df['date'] = pd.to_datetime(df['date'] + '-2019', format='%d-%b-%Y')
print (df)
date
0 2019-10-10
1 2019-10-11

Anomaly using numPy datetime64 to work with dates in a pandas Dataframe column

Cannot covert 'YYYY-MM' string to YYYY-MM datetime using datetime64 for data in pandas DataFrame.
np.datetime64 works to convert date string(s) of 'YYYY-MM' to datetime when stored in a scalar or array, but not when same data is accessed via a DateFrame.
What I want to do is convert a column dates (format: YYYY-MM) to datetime data (with or without adding another column).
csv file data:
month, volume
2019-01, 100
2019-02, 110
Sample Code:
import pandas as pd
import numpy as np
df=pd.read_csv (r'file location')
df["date"]=df["month"].apply(np.datetime64)
# Input (month): 2013-01
# Expected output (date): 2013-01
# Actual output (date): 2013-01-01
So, the datetime64 changes YYYY-MM to YYYY_MM-01
(Also, YYYY is converted to YYYY-01-01)
Perhaps you're looking for pd.Period:
In [11]: df.date.apply(pd.Period, freq='M')
Out[11]:
0 2019-01
1 2019-02
Name: date, dtype: object
Similarly, but without the apply:
In [12]: pd.to_datetime(df.date).dt.to_period(freq='M')
Out[12]:
0 2019-01
1 2019-02
Name: date, dtype: object