mydict = [{'Counted number': '26', 'Timestamp': '8/10/2015 13:07:38'},{'Counted number': '14','Timestamp': '8/10/2015 11:51:14'},{'Counted number': '28','Timestamp': '8/10/2015 13:06:27'}, {'Counted number': '20','Timestamp': '8/10/2015 12:53:42'}]
How to sort this dict based on timestamp?
This should work
import time
mydict.sort(key=lambda x:time.mktime(time.strptime(x['Timestamp'], '%d/%m/%Y %H:%M:%S')))
mydict.sort(key=lambda x:x['Timestamp'])
This will compare the elements of mydict based on their time stamp and sort it that way. Now, if you want to sort it by the actual time, then you have to convert that timestamp string to a Time object of some sort, and then sort mydict based on that. This question will likely help with that.
Related
I am currently updating an script, which was written using datetime library
from datetime import datetime and timedelta
One of the cool features of datetime is the function that returns you a datetime feature (suchs as .day, .year, .hour). eg:
datetime(1860,01,01).year
>1980
datetime(1860,01,01).month
>1
Now I expect that given a numpy.datetime64 array I can extract the datetime feature I am interested. The question is: Is there a numpy object that returns an array of the requested feature?. For instance:
#Given an input np.datetime64 array:
arr = array(['1981-01-01T00:00:00.000000', '1981-01-02T00:00:00.000000',
'1981-01-03T00:00:00.000000', '1981-01-04T00:00:00.000000',
'1981-01-05T00:00:00.000000'], dtype='datetime64[us]')
# I am trying to get all the months:
arr.months
>np.array(['01', '01', '01', '01','01'])
arr.days
>np.array(['01', '02', '03', '04','05'])
This is critical for the rework of my script but I haven't found any solution yet, please help me :)
Thanks a lot!
Joues
Im trying to provide dynamic value to relativedelta function, i.e relativedelta(days=1) i would like to assign dynamic function value days, months, years to the function. Consider my situation as follows.
I will get list dynamicaly as follows:
Ex: 1
list = ['today', 'minus', '1', 'days']
Ex: 2
list = ['today', 'plus', '1', 'year']
Ex: 3
list = ['today', 'plus', '1', 'months']
I wrote my code to handle the calculation
import operator
from datetime import datetime
from datetime import date
from dateutil.relativedelta import relativedelta
operations = {
'plus': operator.add,
'minus': operator.sub,
}
today = date.today()
new_date = self.operations['plus'](today, relativedelta(days=1))
# the above is some thing like [today + relativedelta(days=1)]
What I'm trying to do is like operations I would like to assign days, months, years to the relativedelta() function, but I couldn't able to do it. Any suggested way to do it?
Found a way to do it!
We can use the **expression call syntax to pass in a dictionary to a function instead, it'll be expanded into keyword arguments (which your **kwargs function parameter will capture again):
attributes = {'days': 1}
relativedelta(**attributes)
I have a dataframe that looks like this, but with multiple records:
ID Date
1 {'day': 20, 'year': 2018, 'month':9}
I am trying to change everything in the Date column in to pandas timeseries format. I was trying to loop through the data and change each entry by doing the following but am getting an error saying that the formats don't match.
for index, rows in iterrows:
x = row['Date']
pd.to_datetime(pd.Series(x), format = 'day': %d, 'year': %y, \
'month': %m, dayfirst = True)
When running df.to_dict(), this is the output:
{'ID': {0: '1'}, 'Date':{0: "{'day': 20, 'year': 2018, 'month': 9}"}}
Steps
Convert column of dictionaries to list of dictionaries
Convert list of dictionaries to DataFrame
Pass DataFrame to pd.to_datetime - This is the cool part! pd.to_datetime accepts a DataFrame if there are appropriately named columns. So happens, your dictionaries have the right keys that will get parsed as column names in step 2.
Assign to 'Date'
df.assign(Date=pd.to_datetime(pd.DataFrame(df.Date.tolist())))
ID Date
0 1 2018-09-20
I was able to solve it with this code:
df['Date'] = pd.to_datetime(df['Date'],format="{'day': %d, 'year': %Y, 'month': %m}")
There should not have been pd.Series.
I have a list with multiple dictionaries, like the following:
[{'Date': '6-1-2017', 'Rate':'0.3', 'Type':'A'},
{'Date': '6-1-2017', 'Rate':'0.4', 'Type':'B'},
{'Date': '6-1-2017', 'Rate':'0.6', 'Type':'F'},
{'Date': '6-1-2017', 'Rate':'0.1', 'Type':'B'}
]
I would now like to change the dates, because they need to be in the format 'yyymmdd', which starts at 1900-01-01. In other words, I would like to change the '6-1-2017' to '1170106'.
As this has to be done every week (with the then current date), I do not want to change this by hand. So next week, '13-1-2017' has to be transformed into '1170113'.
Anyone ideas how to do this? I have tried several things, but I can't even get my code to select the date-values of all dictionaries.
Many thanks!
You can use the datetime module, which provides a lot of functionality to manipulate datetime objects including converting datetime to string and the way back, accessing different components of the datetime object, etc:
from datetime import datetime
for l in lst:
l['Date'] = datetime.strptime(l['Date'], "%d-%m-%Y")
l['Date'] = str(l['Date'].year - 1900) + l['Date'].strftime("%m%d")
lst
#[{'Date': '1170106', 'Rate': '0.3', 'Type': 'A'},
# {'Date': '1170106', 'Rate': '0.4', 'Type': 'B'},
# {'Date': '1170106', 'Rate': '0.6', 'Type': 'F'},
# {'Date': '1170106', 'Rate': '0.1', 'Type': 'B'}]
I've data like this.
startDateTime: {'timeZoneID': 'America/New_York', 'date': {'year': '2014', 'day': '29', 'month': '1'}, 'second': '0', 'hour': '12', 'minute': '0'}
This is just a representation for 1 attribute. Like this i've 5 other attributes. LastModified, created etc.
I wanted to derive this as ISO Date format yyyy-mm-dd hh:mi:ss. is this the right way for doing this?
def parse_date(datecol):
x=datecol;
y=str(x.get('date').get('year'))+'-'+str(x.get('date').get('month')).zfill(2)+'-'+str(x.get('date').get('day')).zfill(2)+' '+str(x.get('hour')).zfill(2)+':'+str(x.get('minute')).zfill(2)+':'+str(x.get('second')).zfill(2)
print y;
return;
That works, but I'd say it's cleaner to use the string formatting operator here:
def parse_date(c):
d = c["date"]
print "%04d-%02d-%02d %02d:%02d:%02d" % tuple(map(str, (d["year"], d["month"], d["day"], c["hour"], c["minute"], c["second"])))
Alternatively, you can use the time module to convert your fields into a Python time value, and then format that using strftime. Remember the time zone, though.